jq命令10分钟速成

示例json:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
{
"status": "ok",
"data": [{
"id": 1,
"name": "zhangsan"
},
{
"id": 2,
"name": "wangwu"
}
]
}
{ "status": "ok", "data": [{ "id": 1, "name": "zhangsan" }, { "id": 2, "name": "wangwu" } ] }
{
  "status": "ok",
  "data": [{
      "id": 1,
      "name": "zhangsan"
    },
    {
      "id": 2,
      "name": "wangwu"
    }
  ]
}

1 直接格式化打印

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
at ./test.json | jq .
{
"status": "ok",
"data": [
{
"id": 1,
"name": "zhangsan"
},
{
"id": 2,
"name": "wangwu"
}
]
}
at ./test.json | jq . { "status": "ok", "data": [ { "id": 1, "name": "zhangsan" }, { "id": 2, "name": "wangwu" } ] }
at ./test.json | jq .
{
  "status": "ok",
  "data": [
    {
      "id": 1,
      "name": "zhangsan"
    },
    {
      "id": 2,
      "name": "wangwu"
    }
  ]
}

2 直接取map的某个元素

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat ./test.json | jq .status
"ok"
cat ./test.json | jq .status "ok"
cat ./test.json | jq .status
"ok"

3 取某个数组中所有的key对应元素

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
cat ./test.json | jq ".data[].name"
"zhangsan"
"wangwu"
cat ./test.json | jq ".data[].name" "zhangsan" "wangwu"
cat ./test.json | jq ".data[].name"
"zhangsan"
"wangwu"

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *