gpt4 book ai didi

json - 使用 jq 从嵌套的 JSON 对象中提取选定的属性

转载 作者:行者123 更新时间:2023-12-04 01:51:52 26 4
gpt4 key购买 nike

给定一个 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"
}
}
]

我想从嵌套的 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对象被过滤为仅包含 NAMENAME_LONG特性。

关于json - 使用 jq 从嵌套的 JSON 对象中提取选定的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29259249/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com