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


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

    jq命令的帮助信息:

    abby@abby: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
        filter's results as JSON on standard output.
        The simplest filter is ., which is the identity filter,
        copying jq's 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 (don't 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会将json格式化为多行树状结构输出,但有时需要将一个json串在一行输出,可使用-c参数,例如

    jq -c . xxx.json 

    #用逗号分隔可以同时获取json中多个key的值。但过滤出的多个值会分多行显示。要注意除了逗号之外不能有其他字符(包括空格),例如

    js .field1,.field2 xxx.json

    使用:

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

    例子:

    原始数据:

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

    格式化:

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

    选取某属性:

    abby@abby:bgs$ head -n2 bgs_enterprise_info_norm.json | jq .industry
    "纺织服装、服饰业"
    "批发业"
    abby@abby:bgs$ jq .description bgs_enterprise_info_norm.json | grep 空值 | wc -l
    2770786

    内建函数
    jq还有一些内建函数如 key,has
    key是用来获取JSON中的key元素的:

    cat json_raw.txt | jq 'keys'
    [
      "employees",
      "location",
      "name"
    ]

    has是用来是判断是否存在某个key: 

    cat json_raw.txt | jq 'has("name")'
    true
    
    cat json_raw.txt | jq 'has("noexisted")'
    false

    参考:

    linux 命令之jq

    jq : Linux下json的命令行工具

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

     jq : Linux下json的命令行工具-Bean_lee-ChinaUnix博客

  • 相关阅读:
    Linux 安装apache2时候遇到的问题
    ubuntu安装php时出现error: xml2config not found. Please check your libxml2 installation 的解决办法
    Linux一块网卡绑定2个IP地址
    php 满足条件后跳转的几种方法.
    Linux 安装php 遇到 libtool: link: `ext/date/php_date.lo' is not a valid libtool object
    js 光标移动到输入框最后位置函数
    Linux iptables 防火墙相关命令介绍及使用
    tcpdump的简单选项介绍
    子网掩码转换函数
    Linux 十六进制转换十进制的函数
  • 原文地址:https://www.cnblogs.com/bymo/p/7484023.html
Copyright © 2020-2023  润新知