tidwall / gjson

Get JSON values quickly - JSON parser for Go
MIT License
14.4k stars 858 forks source link

Using modifiers with curly brackets in array of struct changes the key to the modifier name #368

Closed egjlmn1 closed 2 months ago

egjlmn1 commented 2 months ago

Given this json

{
  "name": {"first": "Tom", "last": "Anderson"},
  "age":37,
  "children": ["Sara","Alex","Jack"],
  "fav.movie": "Deer Hunter",
  "friends": [
    {"first": "Dale", "last": "Murphy", "age": 44},
    {"first": "Roger", "last": "Craig", "age": 68},
    {"first": "Jane", "last": "Murphy", "age": 47}
  ]
}

And this path: friends.#.{first,age.@tostr} I want to get [{"first":"Dale","age":"44"},{"first":"Roger","age":"68"},{"first":"Jane","age":"47"}] But getting instead [{"first":"Dale","@tostr":"44"},{"first":"Roger","@tostr":"68"},{"first":"Jane","@tostr":"47"}]

I'm guessing this is a bug, is there another way to get what I want?

egjlmn1 commented 2 months ago

can just do it with friends.#.{first,"age":age.@tostr} but still weird that thats the output...

volans- commented 2 months ago

@egjlmn1 as mentioned in the multipaths documentation you can specify the name of the fields:

friends.#.{first,"age":age.@tostr}

P.S. I see you found the answer while I was writing :)