跳到主內容

【jq】 linux json 查詢工具(查詢篇)

  • data
{
  "firstName": "John",
  "lastName": "Smith",
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021"
  },
  "phoneNumber": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "fax",
      "number": "646 555-4567"
    }
  ],
  "gender": {
    "type": "male"
  }
}
  • 取得資料(age)
[root@jylee-stres-stest250 tmp]# cat data.json | jq .age

25
  • 取得資料(city) (addres > city)
[root@jylee-stres-stest250 tmp]# cat data.json | jq .address.city
"New York"
  • 輸出不帶雙引號 -r :
[root@jylee-stres-stest250 tmp]# cat data.json | jq .firstName
"John"

[root@jylee-stres-stest250 tmp]# cat data.json | jq -r .firstName
John
  • 取得資料(city) (addres > city)
[root@jylee-stres-stest250 tmp]# cat data.json | jq .address.city
"New York"
  • 取得電話
# 取得電話陣列
[root@jylee-stres-stest250 tmp]# cat data.json | jq .phoneNumber
[
  {
    "type": "home",
    "number": "212 555-1234"
  },
  {
    "type": "fax",
    "number": "646 555-4567"
  }
]



#取得第一組電話
[root@jylee-stres-stest250 tmp]# cat data.json | jq .phoneNumber[0]
{
  "type": "home",
  "number": "212 555-1234"
}

#取得第一組電話( type + number )
[root@jylee-stres-stest250 tmp]#cat data.json | jq -r '.phoneNumber[0] | .type+","+.number'
home,212 555-1234

#取得所有type
[root@jylee-stres-stest250 tmp]# cat data.json | jq '.phoneNumber[] | .type'
"home"
"fax"
  • 使用map,join重組資料
[root@jylee-stres-stest250 tmp]# cat data.json | jq '.phoneNumber | map(.type)' 
[
  "home",
  "fax"
]
[root@jylee-stres-stest250 tmp]# cat data.json | jq '.phoneNumber | map(.type) | join(",")' 
"home,fax"
  • 使用select 過濾資料
[root@jylee-stres-stest250 tmp]# cat sample.json | jq -r '.phoneNumber[] | select(.type == "home") '
{
  "type": "home",
  "number": "212 555-1234"
}

[root@jylee-stres-stest250 tmp]# cat sample.json | jq -r '.phoneNumber[] | select(.type == "home") | .number'
212 555-1234
  • 依key取出值
# 回傳物件
[root@jylee-stres-stest250 tmp]# echo '{"a":1, "ip":["10.10.10.1","10.10.10.2"], "c":3, "d":4}' | jq '{ip, d}'

{
  "ip": [
    "10.10.10.1",
    "10.10.10.2"
  ],
  "d": 4
}

# 回傳傳列
[root@jylee-stres-stest250 tmp]# echo '{"a":1, "ip":["10.10.10.1","10.10.10.2"], "c":3, "d":4}' | jq '[{ip, d}]'

[
  {
    "ip": [
      "10.10.10.1",
      "10.10.10.2"
    ],
    "d": 4
  }
]
# 綜合查詢
# @timestamp > 2025-08-20T16:, http_status 5開頭
jq -r 'select((.["@timestamp"] | startswith("2025-08-20T16:"))
              and ((.http_status // .status | tostring) | test("^5")))
       | [."@timestamp", (.http_status // .status | tostring), .request_uri] | @tsv' \
  access.log-20250821
  
2025-08-20T16:15:59+08:00	500	/search/searchShop.jsp?keyword=%E6%A5%B5%E8%87%B4%E9%A2%A8%E6%A0%BC%20%E7%9A%AE%E8%B3%AA%E5%AE%A4%E5%85%A7%E6%8B%96%E9%9E%8B
2025-08-20T16:15:59+08:00	500	/search/_next/static/css/ed87e7f553f8bcd9.css
2025-08-20T16:16:00+08:00	500	/search/searchShop.jsp?keyword=%E5%B9%B3%E6%9D%BF%E9%9B%BB%E8%85%A6
2025-08-20T16:16:00+08:00	500	/search/_next/static/chunks/393-eda0e0e00dcf367e.js
2025-08-20T16:16:00+08:00	500	/search/_next/static/media/momo-icon.42337f3f.ttf
2025-08-20T16:16:00+08:00	500	/search/_next/static/chunks/webpack-b9b83558616d5724.js
2025-08-20T16:16:00+08:00	500	/search/_next/static/chunks/main-app-553b0eca9bdb3ac5.js

jq_array_to_shell


data

{
  "values": [
        {
          "email": "user1@domain.com",
          "id": "USER1_ID"
        },{
          "email": "user2@domain.com",
          "id": "USER2_ID"
        }
	]
}

ss.sh

while IFS=',' read -r email id; do
  echo "$email"
  echo "$id"
done <<EOF
$(jq -r '.values[] | "\(.email),\(.id)"' data2.json)
EOF
[root@jylee-stres-stest250 tmp]# sh ss.sh
user1@domain.com
USER1_ID
user2@domain.com
USER2_ID

https://stedolan.github.io/jq/manual/#Invokingjq