如何使用jq从json获取属性

时间:2018-11-19 06:01:03

标签: shell unix jq

我已经编写了一个bash文件来从json获取属性值。但是它说没有显示数据。不确定如何使jq在我的Linux机器中工作。有人可以帮忙吗?

 curl -v -H "Content-Type: application/json"  -X GET  'URL'  > myFile.json

    cat myFile.json | jq '.offerVerticalMap["GROCERIES.offerPublishStatus"]'

在myFile.json中,正在创建json,但无法提取属性值。

请有人最早调查这个吗?

 offerVerticalMap": {
"GROCERIES": {
"vertical": "GROCERIES",
"offerPublishStatus": "PUBLISHED",
"targetStatus": "IN_PROGRESS",
"statusChangeReasons": { },
"statusChangedBy": "GateKeeper",
"publishStatusOverridden": false,
"publishStatusChangeDtm": 1539062374714,
"verticalEligibility": true,
"verticalEligibilityChangeReasons": {
"GE_SERVICE": "[[Global Eligibility]]"
},
"verticalEligibilityChangeDtm": 1542349224297
},

这是我的json,我需要拉出offerPublishStatus

谢谢

1 个答案:

答案 0 :(得分:2)

您的json仍然显示为无效。无论如何,我将附加一个示例json以及如何通过jq提取它。请看下面:

~$ cat n1.json
[  
   {  
      "name":"sandboxserver.tar.gz.part-aa",
      "hash":"010d126f8ccf199f3cd5f468a90d5ae1",
      "bytes":4294967296,
      "last_modified":"2018-10-10T01:32:00.069000",
      "content_type":"binary/octet-stream"
   },
   {  
      "name":"sandboxserver.tar.gz.part-ab",
      "hash":"49a6f22068228f51488559c096aa06ce",
      "bytes":397973601,
      "last_modified":"2018-10-10T01:32:22.395000",
      "content_type":"binary/octet-stream"
   },
   {  
      "name":"sandboxserver.tar.gz.part-ac",
      "hash":"2c5e845f46357e203214592332774f4c",
      "bytes":5179281858,
      "last_modified":"2018-10-11T08:20:11.566000",
      "content_type":"binary/octet-stream"
   }
]

然后使用jq查找名称:

~$ jq -r '.[].name' n1.json
sandboxserver.tar.gz.part-aa
sandboxserver.tar.gz.part-ab
sandboxserver.tar.gz.part-ac

让我知道这是否有帮助。