我有一个json输出
{
"code": 200,
"success": true,
"data": [{
"id": "66",
"subnet": "0.0.0.0",
"mask": "",
"sectionId": "1",
"description": "THA4",
},
{
"id": "72",
"subnet": "1.2.3.4",
"mask": "",
"sectionId": "1",
"description": "ALF5",
},
],
}
我想基于描述和子网获得如下输出:
THA4
0.0.0.0
ALF5
1.2.3.4
,并且也想要这样:
THA4,0.0.0.0
ALF5,1.2.3.4
关于我的查询:
jq '.data | .[].description,.[].subnet'
我只能这样获得:
0.0.0.0
1.2.3.4
THA4
ALF5
答案 0 :(得分:1)
严格来说,样本“ JSON”不是有效的JSON。以下假定多余的逗号已被删除。
要获得所需的线性列表,只需将[]
吊起:
jq -r'.data [] | (.description,.subnet)'
产生:
THA4
0.0.0.0
ALF5
1.2.3.4
获取CSV的一种方法是使用@csv
:
.data [] | [.description,.subnet] | @csv
产生引用值:
"THA4","0.0.0.0"
"ALF5","1.2.3.4"
.data[] | "\(.description), \(.subnet)"