[Linux] jq:命令行JSON处理工具

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Linux] jq:命令行JSON处理工具相关的知识,希望对你有一定的参考价值。

jq命令帮助我们很方便地在终端查看和处理json文件

 

jq命令的帮助信息:

[email protected]:bgs$ jq -h
jq - commandline JSON processor [version 1.5-1-a5b5cbe]
Usage: jq [options] <jq filter> [file...]

    jq is a tool for processing JSON inputs, applying the
    given filter to its JSON text inputs and producing the
    filters results as JSON on standard output.
    The simplest filter is ., which is the identity filter,
    copying jqs input to its output unmodified (except for
    formatting).
    For more advanced filters see the jq(1) manpage ("man jq")
    and/or https://stedolan.github.io/jq

    Some of the options include:
     -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;
     -r        output raw strings, not JSON texts;
     -R        read raw strings, not JSON texts;
     -C        colorize JSON;
     -M        monochrome (dont colorize JSON);
     -S        sort keys of objects on output;
     --tab    use tabs for indentation;
     --arg a v    set variable $a to value <v>;
     --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>;
    See the manpage for more options.

 

使用:

$ jq . xxx.json   # 直接将文件名传给 jq
$ cat bgs_enterprise_info_norm.json | jq .  #或者由其他命令读出文件内容,并传给 jq

 

例子:

原始数据:

[email protected]:bgs$ head -n2 bgs_enterprise_info_norm.json
{"industry": "纺织服装、服饰业", "name": "xxx公司", "business": "xxxxx", "description": "空值"}
{"industry": "批发业", "name": "xxx公司", "business": "xxxxx", "description": "空值"}

 

格式化:

[email protected]:bgs$ head -n2 bgs_enterprise_info_norm.json | jq .
{
  "industry": "纺织服装、服饰业",
  "name": "xxx公司",
  "business": "xxxxx",
  "description": "空值"
}
{
  "industry": "批发业",
  "name": "xxx公司",
  "business": "xxxxx",
  "description": "空值"
}

 

选取某属性:

[email protected]:bgs$ head -n2 bgs_enterprise_info_norm.json | jq .industry
"纺织服装、服饰业"
"批发业"

 

[email protected]:bgs$ jq .description bgs_enterprise_info_norm.json | grep 空值 | wc -l
2770786

 

参考:

linux 命令之jq

jq : Linux下json的命令行工具

命令行 JSON 处理工具 jq 的使用介绍

 

以上是关于[Linux] jq:命令行JSON处理工具的主要内容,如果未能解决你的问题,请参考以下文章

linux下命令行json工具: jq

一个JSON字符串和文件处理的命令行神器jq,windows和linux都可用

使用 jq 或替代命令行工具比较 JSON 文件

转帖Linux命令行操作json神器jq

jq处理JSON数据

linux使用jq解析json