Zeusox
Zeusox

Reputation: 8448

Mac Terminal: Delete Entire Column From Json File

How can I use mac terminal to delete an entire column from a json File.

The Json structure is as follows:

[{
    "recordid": "6a0a9c66f8e0292a54c9f023c93732f1b41d8943",
    "fields": {
        "city": "Cove",
        "zip": "71937",
        "dst": 1,
        "geopoint": [
            34.398483,
            -94.39398
        ],
        "longitude": -94.39398,
        "state": "AR",
        "latitude": 34.398483,
        "timezone": -6
    },
    "geometry": {
        "type": "Point",
        "coordinates": [
            -94.39398,
            34.398483
        ]
    },
    "record_timestamp": "2018-02-09T09:33:38.603-07:00"
},
{
    "recordid": "37e2c801aafc7befde9734bcb1b1f83a5645ad0f",
    "fields": {
        "city": "Edgemont",
        "zip": "72044",
        "dst": 1,
        "geopoint": [
            35.624351,
            -92.16056
        ],
        "longitude": -92.16056,
        "state": "AR",
        "latitude": 35.624351,
        "timezone": -6
    },
    "geometry": {
        "type": "Point",
        "coordinates": [
            -92.16056,
            35.624351
        ]
    },
    "record_timestamp": "2018-02-09T09:33:38.603-07:00"
}]

Using Terminal, how can I remove both and all columns including geopoint and geometry attributes while saving the file with the rest which I would wanna keep.

Upvotes: 0

Views: 251

Answers (1)

0stone0
0stone0

Reputation: 43934

Use , map trough the JSON, delete .geometry and .fields.geopoint;

jq 'map(del(.fields.geopoint, .geometry))'

Result;

[
  {
    "recordid": "6a0a9c66f8e0292a54c9f023c93732f1b41d8943",
    "fields": {
      "city": "Cove"
    }
  },
  {
    "recordid": "6a0a9c66f8e0292a54c9f023c93732f1b41d8342",
    "fields": {
      "city": "Edgemont"
    }
  }
]
cat json.json | jq 'map(del(.fields.geopoint, .geometry))' > new.json
mv new.json json.json # Overwrites original file

Try it online!


cat mainzip.json | jq 'map(del(.datasetid, .fields.city, .fields.dst, .fields.geopoint, .fields.state, .fields.timezone, .type.point, .geometry, .record_timestamp))' > temporary_mainzip.json
mv temporary_mainzip.json mainzip.json 

Upvotes: 1

Related Questions