如何使用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"
}]
使用终端,如何在保存文件以及我想保留的其余部分的同时删除包括geopoint
和geometry
属性的所有列和所有列。
答案 0 :(得分:1)
使用jq,通过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
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