I want to add an attribute in many object(situated in an array) and this value will be get dynamically. I use the JSON below, and I already made a query to extract what I want. We will start with th result of this query.
First my entire JSON:
[
{
"Nature":"lol",
"EV":"lol",
"Moves":[
{
"Move":"OHKOmove",
"Max":100,
"Min":15
},
{
"Move":"cacaz",
"Max":35,
"Min":20
}
]
},
{
"Nature":"loi",
"EV":"lal",
"Moves":[
{
"Move":"caca1",
"Max":100,
"Min":3
},
{
"Move":"caca2",
"Max":100,
"Min":3
}
]
},
{
"Nature":"loi2",
"EV":"lal",
"Moves":[
{
"Move":"caca1",
"Max":100,
"Min":3
},
{
"Move":"caca2",
"Max":100,
"Min":3
},
{
"Move":"caca3",
"Max":100,
"Min":3
}
]
},
{
"Nature":"loi3",
"EV":"lil",
"Moves":[
{
"Move":"caca1",
"Max":100,
"Min":3
},
{
"Move":"caca2",
"Max":100,
"Min":3
},
{
"Move":"caca3",
"Max":100,
"Min":3
}
]
}
]
Then my query: [?(length(Moves[?Max == `100`]) > `1`)].{Nature: Nature, EV: EV, Moves: Moves[?Max == `100`].Move, MovesCount: length(Moves[?Max == `100`].Move)} | [@,{MaxMouvCount: max_by(@, &MovesCount).MovesCount}][]
And the result of my query give this:
JSON Format Example 1
[
{
"Nature": "loi",
"EV": "lal",
"Moves": [
"caca1",
"caca2"
],
"MovesCount": 2
},
{
"Nature": "loi2",
"EV": "lal",
"Moves": [
"caca1",
"caca2",
"caca3"
],
"MovesCount": 3
},
{
"Nature": "loi3",
"EV": "lil",
"Moves": [
"caca1",
"caca2",
"caca3"
],
"MovesCount": 3
},
{
"MaxMouvCount": 3
}
]
The idea is to put the attribute "MaxMouvCount": 3
on each objects in the array and then delete it from the array to give a result like this:
JSON Format Example 2
[
{
"Nature": "loi",
"EV": "lal",
"Moves": [
"caca1",
"caca2"
],
"MovesCount": 2,
"MaxMouvCount": 3
},
{
"Nature": "loi2",
"EV": "lal",
"Moves": [
"caca1",
"caca2",
"caca3"
],
"MovesCount": 3,
"MaxMouvCount": 3
},
{
"Nature": "loi3",
"EV": "lil",
"Moves": [
"caca1",
"caca2",
"caca3"
],
"MovesCount": 3,
"MaxMouvCount": 3
}
]
In the title I talk about array, in fact with .*
after my query I can transform the object in array and maybe put more easier the value in each array(matching with objects) and retransform array into object with object constructor. But I don't know how to do it. Can you help me please or tell me at least if it's possible.
PS: I use only JMESPath so I don't want an answer with any other language which contains JMESPath code(like javascript(in my case) or python or something else)