milami
milami

Reputation: 39

Deleting a specific element from JSON

I'm trying to clean up my json file with jq and remove all the elements that don't belong to the city of Bern. Here is the JSON example:

[
{
    "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"
  }
]

I'm trying to remove all the entries with the city "Ostermundigen" in it. I've tried different things:

jq 'map(del(.vicinity|contains(“Ostermundigen”)))’ Bern.json

this expects some input from me:

(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?



Upvotes: 0

Views: 46

Answers (1)

pmf
pmf

Reputation: 36286

You need select to filter the right items. Without select, the filter would traverse to the criteria and delete just that.

Also, map(del(…)) would leave null values behind. Rather delete from outside the array, and descend to the items using .[] from within del:

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

Upvotes: 1

Related Questions