The library tidwall/gjson allows me to parse json string and apply modifiers on it (a custom function on the value) but I dont see a way to apply a modifier on a value inside an array of structs.
There is a way to add modifiers to values on arrays but it wont return the orignal array with the changed value, it will return a new array with only the changed values.
For example
[
{
"x":5,
"y":7
},
...
]
If I want to create a modifier to add 1 to all the values of x, it will return a list of ints, where the values are the values of x+1 but thats not what i want.
I want a list of the structs of both x and y where only the values of x are changed.
Is there a way to acomplish what I want with gjson? And if not is there another way?
found a way.
Given the json:
{
"age":37,
"friends": [
{"first": "Dale", "last": "Murphy", "age": 44},
{"first": "Roger", "last": "Craig", "age": 68},
{"first": "Jane", "last": "Murphy", "age": 47}
]
}
The path
friends.#.{first,"age":age.@tostr}
will output:
[{"first":"Dale","age":"44"},{"first":"Roger","age":"68"},{"first":"Jane","age":"47"}]
This way I can control what will go into my inner array struct which is exactly what I want.
Note that the "age":
part is crucial, and if I don’t set it, the output will look like this:
[{"first":"Dale","@tostr":"44"},{"first":"Roger","@tostr":"68"},{"first":"Jane","@tostr":"47"}]
It’s weird and I think that is a bug, but I found a workaround so oh well…