如何使用 JMESPATH 合并 JSON?
How to merge JSON using JMESPATH?
所以问题是我 JSON 看起来有点像这样:
{
"country": "USA",
"states": [
{
"stateName": "California",
"timeZone": "UTC-8",
"cities": [
{
"cityName": "Los Angeles"
},
{
"cityName": "San Diego"
}
]
},
{
"stateName": "Texas",
"timeZone": "UTC-8",
"cities": [
{
"cityName": "Houston"
},
{
"cityName": "Dallas"
}
]
}
]
}
(那个json只是一个例子,不要当真)
我需要做的是列出所有城市,同时从父项中获取 "stateName" 字段并以某种方式将其放入城市对象中,结果如下所示:
[
{
"stateName": "California",
"cityName": "Los Angeles"
},
{
"stateName": "Texas",
"cityName": "Houston"
},
{
"stateName": "Texas",
"cityName": "Dallas"
},
{
"stateName": "California",
"cityName": "San Diego"
}
]
我尝试以几种不同的方式使用内置的合并功能,但没有得到任何结果,此外关于该功能的文档不是很直观。
我的一次失败尝试:
merge(`states[].cities[]`, `states[].{stateName: stateName}`)
有什么想法吗?
提前致谢。
您不能使用 jmespath 执行此操作。检查一下:https://github.com/jmespath/jmespath.js/issues/22#issuecomment-394157797
不过,您现在可以这样做:
states[].{stateName: stateName, cities: cities[].cityName}[]
得到如下输出:
[
{
"stateName": "California",
"cities": [
"Los Angeles",
"San Diego"
]
},
{
"stateName": "Texas",
"cities": [
"Houston",
"Dallas"
]
}
]
我找到了解决方案,但您需要在此处输入一些 hard-coded 值作为查询:
states[].{stateName: stateName, cities: cities[]}[] | @[*].{stateName: {stateName: stateName}, cities: cities} | @[*].[merge(stateName,cities[0]),merge(stateName,cities[1])][]
它给出:
[
{
"stateName": "California",
"cityName": "Los Angeles"
},
{
"stateName": "California",
"cityName": "San Diego"
},
{
"stateName": "Texas",
"cityName": "Houston"
},
{
"stateName": "Texas",
"cityName": "Dallas"
}
]
所以问题是我 JSON 看起来有点像这样:
{
"country": "USA",
"states": [
{
"stateName": "California",
"timeZone": "UTC-8",
"cities": [
{
"cityName": "Los Angeles"
},
{
"cityName": "San Diego"
}
]
},
{
"stateName": "Texas",
"timeZone": "UTC-8",
"cities": [
{
"cityName": "Houston"
},
{
"cityName": "Dallas"
}
]
}
]
}
(那个json只是一个例子,不要当真)
我需要做的是列出所有城市,同时从父项中获取 "stateName" 字段并以某种方式将其放入城市对象中,结果如下所示:
[
{
"stateName": "California",
"cityName": "Los Angeles"
},
{
"stateName": "Texas",
"cityName": "Houston"
},
{
"stateName": "Texas",
"cityName": "Dallas"
},
{
"stateName": "California",
"cityName": "San Diego"
}
]
我尝试以几种不同的方式使用内置的合并功能,但没有得到任何结果,此外关于该功能的文档不是很直观。
我的一次失败尝试:
merge(`states[].cities[]`, `states[].{stateName: stateName}`)
有什么想法吗?
提前致谢。
您不能使用 jmespath 执行此操作。检查一下:https://github.com/jmespath/jmespath.js/issues/22#issuecomment-394157797
不过,您现在可以这样做:
states[].{stateName: stateName, cities: cities[].cityName}[]
得到如下输出:
[
{
"stateName": "California",
"cities": [
"Los Angeles",
"San Diego"
]
},
{
"stateName": "Texas",
"cities": [
"Houston",
"Dallas"
]
}
]
我找到了解决方案,但您需要在此处输入一些 hard-coded 值作为查询:
states[].{stateName: stateName, cities: cities[]}[] | @[*].{stateName: {stateName: stateName}, cities: cities} | @[*].[merge(stateName,cities[0]),merge(stateName,cities[1])][]
它给出:
[
{
"stateName": "California",
"cityName": "Los Angeles"
},
{
"stateName": "California",
"cityName": "San Diego"
},
{
"stateName": "Texas",
"cityName": "Houston"
},
{
"stateName": "Texas",
"cityName": "Dallas"
}
]