linux 之 jq

时间:2025-01-14 23:37:44

1、安装

mac 安装:
brew install jq centos 安装:
yum install jq ubuntu: 安装:
apt-get install jq

2、使用

cat test.json

{
"Type": "online_confirm_order_bill",
"Source": "MEITUAN",
"AreaCode": 2,
"OutCode": "14769782825369498",
"OutStoreCode": "586",
"OnlineChannelCode": 2,
"OrderCreateDate": "2018-12-18 17:50:08",
"Items": [
{
"OutCommodityCode": "2035253",
"CommodityQty": 1,
"CommodityName": "品胜1A充电器",
"CommodityPrice": 3800,
"AvailableNum": 6
},
{
"OutCommodityCode": "2040664",
"CommodityQty": 1,
"CommodityName": "Z品胜双面USB苹果充电线",
"CommodityPrice": 3600,
"AvailableNum": 3
}
]
}

  

jq . test.json

{
"Type": "online_confirm_order_bill",
"Source": "MEITUAN",
"AreaCode": 2,
"OutCode": "14769782825369498",
"OutStoreCode": "586",
"OnlineChannelCode": 2,
"OrderCreateDate": "2018-12-18 17:50:08",
"Items": [
{
"OutCommodityCode": "2035253",
"CommodityQty": 1,
"CommodityName": "品胜1A充电器",
"CommodityPrice": 3800,
"AvailableNum": 6
},
{
"OutCommodityCode": "2040664",
"CommodityQty": 1,
"CommodityName": "Z品胜双面USB苹果充电线",
"CommodityPrice": 3600,
"AvailableNum": 3
}
]
}

  

jq .Type test.json

online_confirm_order_bill

  

jq .Items[] test.json

{
"OutCommodityCode": "2035253",
"CommodityQty": 1,
"CommodityName": "品胜1A充电器",
"CommodityPrice": 3800,
"AvailableNum": 6
}
{
"OutCommodityCode": "2040664",
"CommodityQty": 1,
"CommodityName": "Z品胜双面USB苹果充电线",
"CommodityPrice": 3600,
"AvailableNum": 3
}

  

jq .Items[].OutCommodityCode test.json

"2035253"
"2040664"

  

jq .Items[1].OutCommodityCode test.json

"2040664"

  

jq -r '.Type,.Source' test.json

online_confirm_order_bill
MEITUAN

  

cat test.json | jq -r '.Type'

online_confirm_order_bill

 

cat test.json |jq -c '.Items[]|select(.OutCommodityCode | contains("2035253"))'

{"OutCommodityCode":"2035253","CommodityQty":1,"CommodityName":"品胜1A充电器","CommodityPrice":3800,"AvailableNum":6}

  

cat test.json |jq -c '.Items[]|select(.OutCommodityCode | contains("2035253"))'|jq -r '.CommodityPrice'

3800

  

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

https://github.com/stedolan/jq/wiki/Cookbook