Shell命令行处理JSON

2019-05-09 13:43:09
安装JQ
1
2
wget -O /usr/bin/jq https://github.com/stedolan/jq/releases/download/jq-1.6/jq-linux64
chmod +x /usr/bin/jq
常用举例
  • 原始JSON数据

    1
    {"Rd":"android","Ru":"orginfo","service_type":"","level":"info","Rs":"xzn","R":"MsgInfo","runtime":45,"date":"2019-05-08 23:59:58","Rversion":"6.7","clientip":"140.97.127.275"}
  • 格式化输出

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [root@localhost tmp]# jq -r . abc.json 
    {
    "Rd": "android",
    "Ru": "orginfo",
    "service_type": "",
    "level": "info",
    "Rs": "xzn",
    "R": "MsgInfo",
    "runtime": 45,
    "date": "2019-05-08 23:59:58",
    "Rversion": "6.7",
    "clientip": "140.97.127.275"
    }
  • 提取json指定字段转csv

    1
    2
    [root@localhost tmp]# jq -r '[.runtime,(.clientip|tostring),(.Rs|tostring),(.R|tostring)]|join(",")' abc.json       
    45,140.97.127.275,xzn,MsgInfo
  • 复杂json数据查询

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    complexJson='{"uid":"u2013","nameInfo":[{"firstName":"java","lastName":"scala"}]}'
    echo ${complexJson} |jq
    {
    "uid": "u2013",
    "nameInfo": [
    {
    "firstName": "java",
    "lastName": "scala"
    }
    ]
    }
    echo ${complexJson} |jq -r '.nameInfo[].lastName'
    结果:scala
    注意:.nameInfo[].lastName是获取.nameInfo[] json array中每个json object的lastName字段。


    $ cat sample.json
    [
    {
    "24hvol": "3.908",
    "code": "AUR"
    }
    ]

    $ cat sample.json |jq -r '.[]["24hvol"]'
    3.908
  • csv文件转json文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    cat csvData.csv
    1478,john,38
    1529,lucy,25
    1673,iris,22
    jq -R -c 'split(",")|{"uid":.[0],"name":.[1],"age":.[2]|tonumber}' csvData.csv > csv2json.json
    cat csv2json.json
    {"uid":"1478","name":"john","age":38}
    {"uid":"1529","name":"lucy","age":25}
    {"uid":"1673","name":"iris","age":22}

ref
JQ使用手册
Error with fields starting with a number