来自curl输出的JQ

时间:2018-06-20 03:09:49

标签: json curl jq

我有一个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 

1 个答案:

答案 0 :(得分:1)

  1. 严格来说,样本“ JSON”不是有效的JSON。以下假定多余的逗号已被删除。

  2. 要获得所需的线性列表,只需将[]吊起:

    jq -r'.data [] | (.description,.subnet)'

产生:

THA4
0.0.0.0
ALF5
1.2.3.4
  1. 获取CSV的一种方法是使用@csv

    .data [] | [.description,.subnet] | @csv

产生引用值:

"THA4","0.0.0.0"
"ALF5","1.2.3.4"
  1. 如果您不希望引用各个值:

.data[] | "\(.description), \(.subnet)"