从JSON中删除特定元素

dpiehjr4  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(164)

我尝试用jq清理json文件,删除所有不属于伯尔尼市的元素。

[
{
    "geometry": {
      "location": {
        "lat": 46.93629499999999,
        "lng": 7.503256800000001
      }
    },
    "name": "La Tana Del Lupo",
    "price_level": 1,
    "rating": 4.4,
    "types": [
      "restaurant",
      "point_of_interest",
      "food",
      "establishment"
    ],
    "user_ratings_total": 84,
    "vicinity": "Dorfstrasse 11, Muri bei Bern"
  },
  {
    "geometry": {
      "location": {
        "lat": 46.93406969999999,
        "lng": 7.503247199999999
      }
    },
    "name": "Rung Reuang",
    "rating": 4.8,
    "types": [
      "restaurant",
      "point_of_interest",
      "food",
      "establishment"
    ],
    "user_ratings_total": 131,
    "vicinity": "Worbstrasse 198, Muri bei Bern"
  }
]

我正在尝试删除所有包含“Ostermundigen”城市的条目。我尝试了不同的方法:
jq 'map(del(.vicinity|contains(“Ostermundigen”)))’ Bern.json
这需要我的一些投入:

(base) dcha3453463$ jq 'map(del(.vicinity|contains(“Ostermundigen”)))’ clean_json/Bern.json

(base) dcha3453463$ jq < clean_json/Bern.json 'del(.[]|select(.vicinity(has"Ostermundigen”)))’
>

(base) dcha3453463$ jq 'del(.[]|select(.vicinity=="Ostermundigen")' clean_json/Bern.json
jq: error: syntax error, unexpected $end, expecting ';' or ')' (Unix shell quoting issues?) at <top-level>, line 1:
del(.[]|select(.vicinity=="Ostermundigen")
jq: 1 compile error
>```

What am I doing wrong?
im9ewurl

im9ewurl1#

您需要select来筛选正确的项目。如果没有select,筛选器将遍历条件并删除它。
同样,map(del(…))会留下null值,而是从数组外部删除,并从del中使用.[]向下查找项:

del(.[] | select(.vicinity|contains("Ostermundigen")))

相关问题