美文网首页
jq 命令介绍

jq 命令介绍

作者: mini鱼 | 来源:发表于2020-11-19 12:30 被阅读0次

jq 命令介绍

简介

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

安装

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

  • redhat系Linux安装
yum install -y jq 
  • Debian、Ubuntu、Linux Mint
sudo apt-get install jq
  • SUSE Linux
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方法介绍

相关文章

  • jq 命令介绍

    jq 命令介绍 简介 jq 是一款命令行下处理 JSON 数据的工具。其可以接受标准输入,命令管道或者文件中的 J...

  • jq 命令

    一、JSON 是什么?[https://www.json.org/json-zh.html] JSON(JavaS...

  • 用jq解析json

    1.Mac下安装jq 2.命令行查看jq帮助 3.jq官方文档网址:http://stedolan.github....

  • shell调用kylo API

    参考:curl命令详解 yum install -y jq

  • linux jq 命令

    jq 命令可以方便快捷的解析 json 格式的数据。下面几个例子简单的记录了 jq 命令的用法。其中 task_f...

  • Linux命令jq

    是什么 jq 是一个轻量级的json处理命令。可以对json数据进行分片、过滤、映射和转换 jq . 对json数...

  • 读取HDFS中的 Json文件 并排序去重求个数

    命令 : 备注: hdfs dfs -cat 是HDFS文件系统里的cat命令 jq 是linux下的 Json解...

  • 给力的linux命令--jq简易教程

    jq简介 jq可以对json数据进行分片、过滤、映射和转换,和sed、awk、grep等命令一样,都可以让你轻松地...

  • JSON解析利器jq安装

    安装jq Ubuntu可以直接用apt安装 mac下安装 centos好像没有直接安装的命令依次运行以下命令 使用...

  • linux 下强大的 JSON 解析命令 jq

    介绍 jq is like sed for JSON data - you can use it to slice...

网友评论

      本文标题:jq 命令介绍

      本文链接:https://www.haomeiwen.com/subject/stgkiktx.html