我在考虑以下的JSON对象数组:
[
{
"index": "index1",
"type": "type1",
"id": "id1",
"fields": {
"deviceOs": [
"Android"
],
"deviceID": [
"deviceID1"
],
"type": [
"type"
],
"country": [
"DE"
]
}
},
{
"index": "index2",
"type": "type2",
"id": "id2",
"fields": {
"deviceOs": [
"Android"
],
"deviceID": [
"deviceID2"
],
"type": [
"type"
],
"country": [
"US"
]
}
}
]
我希望能将其压平,得到如下结果:
[
{
"index": "index1",
"type": "type",
"id": "id1",
"deviceOs": "Android",
"deviceID": "deviceID1",
"country": "DE"
},
{
"index": "index2",
"type": "type",
"id": "id2",
"deviceOs": "Android",
"deviceID": "deviceID2",
"country": "US"
}
]
我正在尝试使用 jq
,但是我未能展开"fields"
。我该怎么做?目前我对命令行工具感兴趣,但我也乐意听取其他建议。
paths
并考虑使用[paths(scalars) as $path | { ($path | map(tostring) | join("_")): getpath($path) } ][]
的结果。 - nik.shornikov[]
替换为| add
,可以得到一个包含所有扁平化路径/值对的对象:[paths(scalars) as $path | { ($path | map(tostring) | join("_")): getpath($path) } ] | add
。 - aRestless