Mac终端:从Json文件删除整个列

时间:2020-03-31 16:33:36

标签: json terminal

如何使用Mac终端从json文件中删除整列。

Json结构如下:

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

使用终端,如何在保存文件以及我想保留的其余部分的同时删除包括geopointgeometry属性的所有列和所有列。

1 个答案:

答案 0 :(得分:1)

使用,通过JSON映射,删除.geometry.fields.geopoint;

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

结果;

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