Turning Array of objects into a bag

Viewed 124

I have a dynamic list from a JSON file that I need to convert. Thanks to the setup I will always get the following output format, but I cannot be certain of the order of the list, just the contents.

[
  {"#text": "Foo"
   "@Name": "Bar"}
  {"#text": "Woo"
   "@Name": "Tar"}
  {"#text": "Fuu"
   "@Name": "Bear"}
]

And I would love to end up with:

{"Foo": "Bar", "Woo": "Tar", "Fuu": "Bear"}

I have tried to use mv-expand, however, I am not able to collect the columns as they are called #text and @Name, meaning that they are invalid to use in a query. I am very new to this language and haven't been able to find anything matching my problem.

1 Answers
print obj = dynamic([
  {"#text": "Foo",
   "@Name": "Bar"},
  {"#text": "Woo",
   "@Name": "Tar"},
  {"#text": "Fuu",
   "@Name": "Bear"}
])
| mv-apply obj on (summarize make_bag(pack(tostring(obj['#text']), tostring(obj['@Name']))))
bag_
{"Foo":"Bar","Woo":"Tar","Fuu":"Bear"}

Fiddle

Related