【问题标题】:How to use jq to fetch attribute from json如何使用jq从json中获取属性
【发布时间】:2018-11-19 06:01:02
【问题描述】:

我编写了一个 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,但无法提取属性值。

请问有人能尽快调查一下吗?

谢谢

【问题讨论】:

  • 请分享myFile.json的样本。
  • 提供了 json @Mayank Porwal
  • 您的 json 似乎无效。请发一份合适的。
  • @MayankPorwal 你现在可以给个建议吗?
  • @user3222718 - 您的 sn-p 仍然不是有效的 JSON。要检查 myFile.json 的有效性,可以使用 jsonlint.com 等在线服务,或运行:jq empty myFile.json

标签: shell unix jq


【解决方案1】:

您的 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

如果这有帮助,请告诉我。

【讨论】:

    猜你喜欢
    • 2014-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-24
    相关资源
    最近更新 更多