Skip to content

1.安装

shell
yum install jq -y
yum install jq -y

2.语法

3.案例

有如下文档a.json:

shell
{
      "action":"DescribeKeyPairsResponse",
      "total_count":2,
      "keypair_set":[
        {
          "description":null,
          "encrypt_method":"ssh-rsa",
          "keypair_name":"kp 1",
          "instance_ids":[
            "i-ogbndull"
          ],
          "create_time":"2013-08-30T05:13:50Z",
          "keypair_id":"kp-bn2n77ow",
          "pub_key":"AAAAB3..."
        },
        {
          "description":null,
          "encrypt_method":"ssh-rsa",
          "keypair_name":"kp 2",
          "create_time":"2013-08-31T05:13:50Z",
          "keypair_id":"kp-b2ivaf15",
          "pub_key":"AAAAB3..."
        }
      ],
      "ret_code":0
    }
{
      "action":"DescribeKeyPairsResponse",
      "total_count":2,
      "keypair_set":[
        {
          "description":null,
          "encrypt_method":"ssh-rsa",
          "keypair_name":"kp 1",
          "instance_ids":[
            "i-ogbndull"
          ],
          "create_time":"2013-08-30T05:13:50Z",
          "keypair_id":"kp-bn2n77ow",
          "pub_key":"AAAAB3..."
        },
        {
          "description":null,
          "encrypt_method":"ssh-rsa",
          "keypair_name":"kp 2",
          "create_time":"2013-08-31T05:13:50Z",
          "keypair_id":"kp-b2ivaf15",
          "pub_key":"AAAAB3..."
        }
      ],
      "ret_code":0
    }
  • 获取值
shell
cat a.json | jq ".keypair_set[].keypair_id"
cat a.json | jq ".keypair_set[].keypair_id"