https://github.com/stedolan/jq
https://stedolan.github.io/jq/manual/
brew install jq
$ cat data.json
{
"code": 0,
"msg": "",
"data": {
"pageNo": 1,
"pageSize": 10,
"total": 5,
"list": [
{
"id": null,
"city": "北京",
"name": null,
"email": null
},
{
"id": null,
"city": "上海",
"name": null,
"email": null
},
{
"id": null,
"city": "广州",
"name": null,
"email": null
},
{
"id": null,
"city": "深圳",
"name": null,
"email": null
},
{
"id": null,
"city": "杭州",
"name": null,
"email": null
}
]
}
}
$ cat data.json | jq .
{
"code": 0,
"msg": "",
"data": {
"pageNo": 1,
"pageSize": 10,
"total": 5,
"list": [
{
"id": null,
"city": "北京",
"name": null,
"email": null
},
{
"id": null,
"city": "上海",
"name": null,
"email": null
},
{
"id": null,
"city": "广州",
"name": null,
"email": null
},
{
"id": null,
"city": "深圳",
"name": null,
"email": null
},
{
"id": null,
"city": "杭州",
"name": null,
"email": null
}
]
}
}
cat data.json | jq .code
0
$ cat data.json | jq .data.list[0]
{
"id": null,
"city": "北京",
"name": null,
"email": null
}
$ cat data.json | jq .data.list[0,1]
{
"id": null,
"city": "北京",
"name": null,
"email": null
}
{
"id": null,
"city": "上海",
"name": null,
"email": null
}
$ cat data.json | jq .data.list[]
{
"id": null,
"city": "北京",
"name": null,
"email": null
}
{
"id": null,
"city": "上海",
"name": null,
"email": null
}
{
"id": null,
"city": "广州",
"name": null,
"email": null
}
{
"id": null,
"city": "深圳",
"name": null,
"email": null
}
{
"id": null,
"city": "杭州",
"name": null,
"email": null
}
$ cat data.json | jq .data.list[].city
"北京"
"上海"
"广州"
"深圳"
"杭州"
列表内的指定字段并删除双引号
$ cat data.json | jq .data.list[].city | tr -d '\"'
北京
上海
广州
深圳
杭州
$ cat data.json | jq 'del(.msg)'
{
"code": 0,
"data": {
"pageNo": 1,
"pageSize": 10,
"total": 5,
"list": [
{
"id": null,
"city": "北京",
"name": null,
"email": null
},
{
"id": null,
"city": "上海",
"name": null,
"email": null
},
{
"id": null,
"city": "广州",
"name": null,
"email": null
},
{
"id": null,
"city": "深圳",
"name": null,
"email": null
},
{
"id": null,
"city": "杭州",
"name": null,
"email": null
}
]
}
}
$ cat data.json | jq 'del(.msg,.data.pageNo,.data.pageSize)'
{
"code": 0,
"data": {
"total": 5,
"list": [
{
"id": null,
"city": "北京",
"name": null,
"email": null
},
{
"id": null,
"city": "上海",
"name": null,
"email": null
},
{
"id": null,
"city": "广州",
"name": null,
"email": null
},
{
"id": null,
"city": "深圳",
"name": null,
"email": null
},
{
"id": null,
"city": "杭州",
"name": null,
"email": null
}
]
}
}
$ cat data.json | jq 'del(.msg,.data.pageNo,.data.pageSize,.data.list[].id)'
{
"code": 0,
"data": {
"total": 5,
"list": [
{
"city": "北京",
"name": null,
"email": null
},
{
"city": "上海",
"name": null,
"email": null
},
{
"city": "广州",
"name": null,
"email": null
},
{
"city": "深圳",
"name": null,
"email": null
},
{
"city": "杭州",
"name": null,
"email": null
}
]
}
}
$ cat data.json | jq 'del(.msg,.data.pageNo,.data.pageSize,.data.list[].id)' | sed 's/杭州/成都/g'
{
"code": 0,
"data": {
"total": 5,
"list": [
{
"city": "北京",
"name": null,
"email": null
},
{
"city": "上海",
"name": null,
"email": null
},
{
"city": "广州",
"name": null,
"email": null
},
{
"city": "深圳",
"name": null,
"email": null
},
{
"city": "成都",
"name": null,
"email": null
}
]
}
}
更多 jq 相关 https://blog.csdn.net/wzj_110/article/details/117387891