Home > other >  Saving a json file after after filtering with jq
Saving a json file after after filtering with jq

Time:12-03

I've filtered values in a json file that I obtained from Google Maps API with the following command:

jq '.[] | select(.vicinity|contains("Bern"))' Bern.json > Bern_test.json

(I only want to have places in Bern)

I tried loading the Bern_test.json file to Tableau to visualize it, but Tableau complains that the format is wrong. json validator online also says something is wrong:


    Error: Parse error on line 38:
    ... Wohlen bei Bern"} { "business_status"
    ----------------------^
    Expecting 'EOF', '}', ',', ']', got '{'

Here is the "broken" part (if it helps):


    "reference": "ChIJ-abNilA5jkcRAh6Jj7dLx_c",
        "scope": "GOOGLE",
        "types": [
            "restaurant",
            "food",
            "point_of_interest",
            "establishment"
        ],
        "vicinity": "Aumattweg 22, Wohlen bei Bern"
    } {
        "business_status": "OPERATIONAL",
        "geometry": {
            "location": {
                "lat": 46.9684408,
                "lng": 7.377661100000001

I don't know why it got broken, or how I can properly save a json file after I've done some magic with jq. Can anyone help?

CodePudding user response:

Your current filter produces a stream of JSON objects rather than a single JSON value. To fix that, wrap your filter in [...]:

jq '[.[] | select(.vicinity|contains("Bern"))]' Bern.json > Bern_test.json

or use the predefined map function to achieve the same result.

jq 'map(select(.vicinity|contains("Bern")))' Bern.json > Bern_test.json
  • Related