jq 命令介绍

2020-11-19  本文已影响0人  mini鱼

jq 命令介绍

简介

jq 是一款命令行下处理 JSON 数据的工具。其可以接受标准输入,命令管道或者文件中的 JSON 数据,经过一系列的过滤器(filters)和表达式的转换后形成我们需要的数据结构并将结果输出到标准输出中。
jq 只能接受 well form 的 JSON 字符串作为输入内容。也就是说输入内容必须严格遵循 JSON 格式的标准。所有的属性名必须是以双引号包括的字符串。对象的最后一个属性的末尾或者数组的最后一个元素的末尾不能有逗号。否则 jq 会抛出无法解析 JSON 的错误。

安装

1. 使用软件包管理器安装

yum install -y jq 
sudo apt-get install jq
sudo zypper install jq

2. 官网下载二进制文件

wget -O jq https://github.com/stedolan/jq/releases/download/jq-1.5/jq-linux64 chmod +x ./jq cp jq /usr/bin

参数说明

选项 解释 备注
-c compact instead of pretty-printed output
-n use null as the single input value
-e set the exit status code based on the output
-s read (slurp) all inputs into an array; apply filter to it 使用-s 选项,jq 会将所有的 JSON 输入放入一个数组中并在这个数组上使用 filter。"-s"选项不但影响到 filter 的写法。如果在 filter 中需要对数据进行选择和映射,其还会影响最终结果。
-r output raw strings, not JSON texts 假设我们要查询 JSON 字符串{"name":"tom"}中 name 的值. 使用-r 选项时返回的是tom. 不使用-r 选项时,返回的是"tom".返回值多了一对双引号
-R read raw strings, not JSON texts
-M monochrome (don't colorize JSON)
-S sort keys of objects on output
--tab use tabs for indentation 使用Tab缩进
--arg a v set variable $a to value <v> 通过该选项提供了和宿主脚本语言交互的能力。该选项将值(v)绑定到一个变量(a)上。在后面的 filter 中可以直接通过变量引用这个值。例如,filter '.$a'表示查询属性名称等于变量 a 的值的属性。
--argjson a v set variable $a to JSON value <v>
--slurpfile a f set variable $a to an array of JSON texts read from <f>
--rawfile a f set variable $a to a string consisting of the contents of <f>
--args remaining arguments are string arguments, not files
--jsonargs remaining arguments are JSON arguments, not files
--

示例

1. 原始数据

cat 1.txt

{
"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"
}
}

2. 格式化输出

# cat 1.txt | jq .
{
  "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"
  }

2、提取字段

获取地址 address 信息:

# cat 1.txt | jq .address
{
  "streetAddress": "21 2nd Street",
  "city": "New York",
  "state": "NY",
  "postalCode": "10021"
}

获取 address 信息中的邮编 postalCode:

# cat 1.txt | jq .address.postalCode
"10021"

3、解析数组中的元素

获取电话信息

# cat 1.txt | jq  .phoneNumber[]
{
  "type": "home",
  "number": "212 555-1234"
}
{
  "type": "fax",
  "number": "646 555-4567"
}

获取第一个电话号码

# cat 1.txt | jq  .phoneNumber[0]
{
  "type": "home",
  "number": "212 55

4、管道操作

假设我只想要家庭电话,而不是整个 JSON 数组数据,可以使用以下方式

# cat 1.txt | jq  '.phoneNumber[] | select(.type == "home") | .number'
"212 555-1234"

提取json指定字段转csv

5、将CSV数据转换为json

csv数据

cat  >> 1.csv  << EOF
1478,john,38
1529,lucy,25
1673,iris,22
EOF

转换格式

# jq -R -c 'split(",")|{"uid":.[0],"name":.[1],"age":.[2]|tonumber}' 1.csv
{"uid":"1478","name":"john","age":38}
{"uid":"1529","name":"lucy","age":25}
{"uid":"1673","name":"iris","age":22}

6、将json转csv

测试数据

cat >> jsonData.json << EOF
{"productId":"2723","click":60,"view":300,"deal":2,"day":"20130919"}
{"productId":"2728","click":130,"view":800,"deal":10,"day":"20130919"}
{"productId":"3609","click":50,"view":400,"deal":3,"day":"20130919"}
{"productId":"3783","click":375,"view":1200,"deal":50,"day":"20130919"}
{"productId":"3522","click":87,"view":573,"deal":15,"day":"20130919"}
EOF

转换格式

# jq -r '[.productId+"_"+.day,(.click|tostring),(.view|tostring),(.deal|tostring)]|join(",")' jsonData.json
2723_20130919,60,300,2
2728_20130919,130,800,10
3609_20130919,50,400,3
3783_20130919,375,1200,50
3522_20130919,87,573,15

参考

官方指导
Parsing JSON with jq
IBM深度介绍
CSDN-Shell命令行处理JSON
JSON介绍
其他格式化json方法介绍

上一篇下一篇

猜你喜欢

热点阅读