我想在许多对象(位于数组中)中添加一个属性,并且该值将动态获取。我使用下面的JSON,并且已经进行了查询以提取所需的内容。我们将从该查询的结果开始。
首先是我的整个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
}
]
}
]
然后我的查询:[?(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}][]
查询的结果如下:
[
{
"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
}
]
想法是将属性"MaxMouvCount": 3
放在数组中的每个对象上,然后将其从数组中删除以得到如下结果:
[
{
"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
}
]
在标题中我谈到了数组,实际上是在查询后使用.*
可以转换数组中的对象,并且可以更容易地将每个数组中的值(与对象匹配)并使用对象构造函数。但是我不知道该怎么做。您能帮我还是至少告诉我是否可能。
PS:我只使用JMESPath,所以我不想用其他包含JMESPath代码的语言(例如javascript(就我而言)或python或其他内容)来回答
答案 0 :(得分:1)
current-node
上下文中缺少引用JSON数据根的令牌python 3.x
使用JMESPath 0.9.4
[但是任何JMESPath引擎都可以使用] 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
设置MaxMouvCount
的值,这在技术上是“作弊”
null
(python恰好将其称为None
)dictionary
在其他情况下称为object
或hash
或associative array
或mapping
JSON Format Example 1
,使其看起来像这样{
"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
}
]
}
尝试02 //第2部分(运行无痛苦的JMESPath查询以获取所需内容)
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
达到了null
(又名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