shell - 如何使用jq从json响应中获取值

时间:2018-05-15 16:00:23

标签: json bash shell curl jq

我需要在bash中使用jq获取“mxa.mailgun.org”

json响应curl请求到api:

{
 "domain": {
    "created_at": "Sun, 13 May 2018 09:48:21 GMT",
    "id": "5af809e51f4b8b00011ee1aa",
    "name": "leadedign.co.uk",
    "require_tls": false,
    "skip_verification": false,
    "smtp_login": "postmaster@leddesign.co.uk",
    "smtp_password": "07d1dee-43f1869c",
    "spam_action": "disabled",
    "state": "unverified",
    "type": "custom",
    "web_prefix": "email",
    "web_scheme": "http",
    "wildcard": false
  },
  "message": "Domain DNS records have been updated",
  "receiving_dns_records": [
    {
      "cached": [
        "1 leadeesign.co.uk"
      ],
      "priority": "10",
      "record_type": "MX",
      "valid": "unknown",
      "value": "mxa.mailgun.org"
    },
    {
      "cached": [
        "1 leaesign.co.uk"
      ],
      "priority": "10",
      "record_type": "MX",
      "valid": "unknown",
      "value": "mxb.mailgun.org"
    }
  ]
  }

我试过了:

curl -X PUT -s --user 'api:key-1659e3c0faff2c4e96e4' -G  https://api.mailgun.net/v3/domains/leadereign.co.uk/verify -d skip=0 -d limit=3 | jq '.[] | {receiving_dns_records: .[]}'

        curl -X PUT -s --user 'api:key-165903eaff2c4e96e4' -G  https://api.mailgun.net/v3/domains/leaderfeign.co.uk/verify -d skip=0 -d limit=3 | jq '.[] | {receiving_dns_records: .[value]}'

PS。出于安全原因,域和api密钥已更改。这个卷曲请求不起作用。

1 个答案:

答案 0 :(得分:0)

你想要"值" 第一个" receiving_dns_records"的值项:

jq -r '.receiving_dns_records[0].value'