使用jq从嵌套的JSON对象中提取所选属性
因此,给定对象的JSON数组:
[ {
"geometry": {
"type": "Polygon",
"coordinates": [[[-69.9969376289999, 12.577582098000036]]]
},
"type": "Feature",
"properties": {
"NAME": "Aruba",
"WB_A2": "AW",
"INCOME_GRP": "2. High income: nonOECD",
"SOV_A3": "NL1",
"CONTINENT": "North America",
"NOTE_ADM0": "Neth.",
"BRK_A3": "ABW",
"TYPE": "Country",
"NAME_LONG": "Aruba"
}
},
{
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[-63.037668423999946, 18.212958075000028]]]
},
"type": "Feature",
"properties": {
"NAME": "Anguilla",
"WB_A2": "-99",
"INCOME_GRP": "3. Upper middle income",
"SOV_A3": "GB1",
"NOTE_ADM0": "U.K.",
"BRK_A3": "AIA",
"TYPE": "Dependency",
"NAME_LONG": "Anguilla"
}
}
]
我想从nested中提取键/值的子集properties
,同时保持外部对象的其他属性不变,产生如下结果:
[ {
"geometry": {
"type": "Polygon",
"coordinates": [[[-69.9969376289999, 12.577582098000036]]]
},
"type": "Feature",
"properties": {
"NAME": "Aruba",
"NAME_LONG": "Aruba"
}
},
{
"geometry": {
"type": "MultiPolygon",
"coordinates": [[[-63.037668423999946, 18.212958075000028]]]
},
"type": "Feature",
"properties": {
"NAME": "Anguilla",
"NAME_LONG": "Anguilla"
}
}
]
即删除除NAME
和以外的所有键NAME_LONG
。
我确信必须有一种使用jq实现此目标的合理简便的方法。帮助表示赞赏。
回答:
您可以使用以下过滤器:
map( .properties |= with_entries(select(.key == ("NAME", "NAME_LONG")))
)
这会映射数组中properties
过滤对象的每个项目,仅包括NAME
和NAME_LONG
属性。
以上是 使用jq从嵌套的JSON对象中提取所选属性 的全部内容, 来源链接: utcz.com/qa/411440.html