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:
[
{
"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:
[
{
"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)
current-node
contextpython 3.x
using JMESPath 0.9.4
[but any JMESPath engine will do]import jmespath
vdata001aa = """<<json.load(JSON Format Example 1)>>"""
vresult = jmespath.compile('@|[*].{"Nature":@.Nature,"EV":@.EV,"Moves":@.Moves,"MovesCount":@.MovesCount,"MaxMouvCount":`3`}').search(vdata001aa)
pprint.pprint(vresult)
[{'EV': 'lal',
'MaxMouvCount': 3,
'Moves': ['caca1', 'caca2'],
'MovesCount': 2,
'Nature': 'loi'},
{'EV': 'lal',
'MaxMouvCount': 3,
'Moves': ['caca1', 'caca2', 'caca3'],
'MovesCount': 3,
'Nature': 'loi2'},
{'EV': 'lil',
'MaxMouvCount': 3,
'Moves': ['caca1', 'caca2', 'caca3'],
'MovesCount': 3,
'Nature': 'loi3'},
{'EV': None,
'MaxMouvCount': 3,
'Moves': None,
'MovesCount': None,
'Nature': None}]
3
for MaxMouvCount
which is technically "cheating"
null
(python happens to call this None
)dictionary
which is known in other contexts as object
or hash
or associative array
or mapping
JSON Format Example 1
so it looks like this instead{
"jsontop": {
"settings_info": {
"MaxMouvCount": 3
},
"nature_table": [
{
"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
}
]
}
Attempt 02 // Part 2 (run the pain-free JMESPath query to get what you want)
import jmespath
vdata001aa = """<<json.load(**RE-NORMALIZED** JSON Format Example 1)>>"""
vresult = jmespath.compile('@|jsontop.nature_table[*].{"Nature":@.Nature,"EV":@.EV,"Moves":@.Moves,"MovesCount":@.MovesCount,"MaxMouvCount":jsontop.settings_info.MaxMouvCount}').search(vdata001aa)
pprint.pprint(vresult)
pass
[{'EV': 'lal',
'MaxMouvCount': None,
'Moves': ['caca1', 'caca2'],
'MovesCount': 2,
'Nature': 'loi'},
{'EV': 'lal',
'MaxMouvCount': None,
'Moves': ['caca1', 'caca2', 'caca3'],
'MovesCount': 3,
'Nature': 'loi2'},
{'EV': 'lil',
'MaxMouvCount': None,
'Moves': ['caca1', 'caca2', 'caca3'],
'MovesCount': 3,
'Nature': 'loi3'}]
None
(aka null
) where we expected 3
import jmespath
vdata001aa = """<<json.load(**RE-NORMALIZED** JSON Format Example 1)>>"""
vresult = jmespath.compile('@|jsontop.nature_table[*].{"Nature":@.Nature,"EV":@.EV,"Moves":@.Moves,"MovesCount":@.MovesCount,"MaxMouvCount":$.jsontop.settings_info.MaxMouvCount}').search(vdata001aa)
pprint.pprint(vresult)
pass