• jq


    1. 简介
    json是一种轻量级的数据交换格式,应用范围非常广泛。在Linux系统下使用jq工具可以非常方便的处理json,官方给的定义是:
     
    A jq program is a “filter”: it takes an input, and produces an output. There are a lot of builtin filters for extracting a particular field of an object, or converting a number to a string, or various other standard tasks.
     
    通俗的说就是一个能够接受json,处理json,输出json的程序,反正很好用。
    安装起来也非常的方便,直接使用yum即可安装。
    [root@test-dhcp ~]# yum install jq
     
    2. 基本操作
    2.1 创建
    [root@test-dhcp ~]# jq -n {a:1} { "a": 1 } 
    [root@test-dhcp ~]# jq -n '{a:"test"}' { "a": "test" }
     
    2.2 合并
    [root@test-dhcp ~]# jq -n '{a:"test"} + {b:2}' { "a": "test", "b": 2 } 
    [root@test-dhcp ~]# jq -n '{a:"test"} + {b:2} + {c:"testc"}' { "a": "test", "b": 2, "c": "testc" }
     
    2.3 删除
    [root@test-dhcp ~]# cat test.json {"a": "test","b": 2, "c": "testc"} 
    [root@test-dhcp ~]# cat test.json |jq . { "a": "test", "b": 2, "c": "testc" }
    [root@test-dhcp ~]# cat test.json |jq 'del(.b)' { "a": "test", "c": "testc" }
     
    2.4 更新
    [root@test-dhcp ~]# cat test.json {"a": "test","b": 2, "c": "testc"} 
    [root@test-dhcp ~]# cat test.json |jq '.b="testb"' { "a": "test", "b": "testb", "c": "testc" }
    [root@test-dhcp ~]# cat test.json |jq '. + {d:4}' { "a": "test", "b": 2, "c": "testc", "d": 4 } [
    root@test-dhcp ~]# cat test.json |jq '. + {d:4}' |jq '.d={dd:5}' { "a": "test", "b": 2, "c": "testc", "d": { "dd": 5 } }
     
    2.5 查询
    [root@test-dhcp ~]# cat test.json |jq . { "a": "test", "b": 2, "c": "testc", "d": { "dd": 5 } } 
    [root@test-dhcp ~]# cat test.json |jq '. + {d:4}' |jq '.d={dd:5}' |jq .d.dd 5 [root@test-dhcp ~]# echo '{"a":1,"b":2}' |jq '[.a,.b]' [ 1, 2 ]
     
    2.6 查看数据类型
    [root@test-dhcp ~]# echo "{}" |jq -r type object 
    [root@test-dhcp ~]# echo '[0, false, [], {}, null, "hello"]' |jq 'map(type)' [ "number", "boolean", "array", "object", "null", "string" ]
     
    2.7 查询数组中的值
    [root@test-dhcp ~]# echo [1,2,3] |jq .[1] 2 
    [root@test-dhcp ~]# echo [1,2,3] |jq .[2] 3 查询数组长度
    [root@test-dhcp ~]# echo [1,2,3,9] |jq '.|length' 4
    [root@test-dhcp ~]# echo [1,2,3] |jq '.|length' 3
     
    2.8 数组相加
    [root@test-dhcp ~]# echo [1,2,3] |jq '. + [4,5,6]' [ 1, 2, 3, 4, 5, 6 ]
     
    2.9 高级查询
    [root@test-dhcp ~]# echo [1,2,3] | jq 'map(select(. >= 2))' [ 2, 3 ] 
    [root@test-dhcp ~]# echo [1,2,3] | jq 'map(select(. == 2))' [ 2 ]
    [root@test-dhcp ~]# echo [1,2,3] | jq 'map(select(. != 2))' [ 1, 3 ]
    [root@test-dhcp ~]# cat test.json [ { "id": "0", "model": "Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz" }, { "id": "1", "model": "Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz" } ]
    [root@test-dhcp ~]# cat test.json |jq .[].model "Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz" "Intel(R)Xeon(R)CPUE5-2620v4@2.10GHz"
     
    2.10 类型转换
    [root@test-dhcp ~]# echo '["a","b,c,d","e"]' |jq 'join(",")' "a,b,c,d,e" 
    [root@test-dhcp ~]# echo '["a","b,c,d","e",1]' |jq 'join(",")' jq: error (at <stdin>:1): string (",") and number (1) cannot be added
    [root@test-dhcp ~]# cat test.json liuxin,30,male jiaweiqiang,29,femal
    [root@test-dhcp ~]# jq -R 'split(",")|{"name":.[0],"age":.[1],"sex":.[2]}' ./test.json { "name": "liuxin", "age": "30", "sex": "male" } { "name": "jiaweiqiang", "age": "29", "sex": "femal" }
    [root@test-dhcp ~]# cat test.json { "name": "liuxin", "age": "30", "sex": "male" } { "name": "jiaweiqiang", "age": "29", "sex": "femal" }
    [root@test-dhcp ~]# cat test.json |jq . -c {"name":"liuxin","age":"30","sex":"male"} {"name":"jiaweiqiang","age":"29","sex":"femal"}
     
     

    喜欢这篇文章?欢迎打赏~~

  • 相关阅读:
    fedora29 安装mongodb 4.0,6问题记录
    乱花渐入迷人眼------从解决jqueryEasyUI上传插件提交ajax请求谈网页调试
    thinkphp5权限仿制
    基于tornado python pandas和bootstrap上传组件的mongodb数据添加工具
    tornado下pandas ndarray乱试
    一个tornado框架下的文件上传案例
    一例tornado框架下利用python panda对数据进行crud操作
    PHP 基础语法实例及注意事项
    转:github使用教程(重装系统后遇到问题该文章帮我解决了)
    ubuntu同步系统时间命令
  • 原文地址:https://www.cnblogs.com/cangqinglang/p/14579349.html
Copyright © 2020-2023  润新知