码农行者 码农行者
首页
  • Python

    • 语言特性
    • Django相关
    • Tornado
    • Celery
  • Golang

    • golang学习笔记
    • 对比python学习go
    • 模块学习
  • JavaScript

    • Javascript
  • 数据结构预算法笔记
  • ATS
  • Mongodb
  • Git
云原生
运维
垃圾佬的快乐
  • 数据库
  • 机器学习
  • 杂谈
  • 面试
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

DeanWu

软件工程师
首页
  • Python

    • 语言特性
    • Django相关
    • Tornado
    • Celery
  • Golang

    • golang学习笔记
    • 对比python学习go
    • 模块学习
  • JavaScript

    • Javascript
  • 数据结构预算法笔记
  • ATS
  • Mongodb
  • Git
云原生
运维
垃圾佬的快乐
  • 数据库
  • 机器学习
  • 杂谈
  • 面试
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Linux基础系列 - jq

    • 介绍
      • 使用
        • 格式化
        • 解析
        • 内建函数
      • 参考
      • 运维
      • linux
      DeanWu
      2018-06-26
      目录

      Linux基础系列 - jq

      # 介绍

      jq 是linux下的一个解析JSON格式字符串的一个命令行工具。可直接使用 yum install jq 来安装。

      jq - commandline JSON processor [version 1.5]
      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.
      
      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
      27

      # 使用

      # 格式化

      [root@pylixm-web ~]# cat test.txt
      {"name":"Google","location":{"street":"1600 Amphitheatre Parkway","city":"Mountain View","state":"California","country":"US"},"employees":[{"name":"Michael","division":"Engineering"},{"name":"Laura","division":"HR"},{"name":"Elise","division":"Marketing"}]}
      
      1
      2

      直接格式化,并校验合法性:

      [root@pylixm-web ~]# cat test.txt |jq .
      {
        "name": "Google",
        "location": {
          "street": "1600 Amphitheatre Parkway",
          "city": "Mountain View",
          "state": "California",
          "country": "US"
        },
        "employees": [
          {
            "name": "Michael",
            "division": "Engineering"
          },
          {
            "name": "Laura",
            "division": "HR"
          },
          {
            "name": "Elise",
            "division": "Marketing"
          }
        ]
      }
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24

      # 解析

      可直接通过key获取到JSON的value

      [root@pylixm-web ~]# cat test.txt |jq .name
      "Google"
      [root@pylixm-web ~]# cat test.txt |jq .test
      null
      
      1
      2
      3
      4

      当key不存在时,会返回null。

      嵌套解析

      [root@pylixm-web ~]# cat test.txt |jq .location.city
      "Mountain View"
      [root@pylixm-web ~]# cat test.txt |jq .employees[]
      {
        "name": "Michael",
        "division": "Engineering"
      }
      {
        "name": "Laura",
        "division": "HR"
      }
      {
        "name": "Elise",
        "division": "Marketing"
      }
      [root@pylixm-web ~]# cat test.txt |jq .employees[0]
      {
        "name": "Michael",
        "division": "Engineering"
      }
      [root@pylixm-web ~]# cat test.txt |jq .employees[0].name
      "Michael"
      
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22

      可使用key 连续获取嵌套的json数据。当值为数组时,可通过下标来获取对应位置的json数据。

      # 内建函数

      keys

      获取最外层的关键字keys,返回一个数组。只能在最外层使用。

      [root@pylixm-web ~]# cat test.txt |jq keys
      [
        "employees",
        "location",
        "name"
      ]
      [root@pylixm-web ~]# cat test.txt |jq .location.keys
      null
      
      1
      2
      3
      4
      5
      6
      7
      8

      has

      判断是否含有某个key,返回一个布尔值的字符串类型。

      [root@pylixm-web ~]# cat test.txt |jq 'has("test")'
      false
      
      1
      2

      # 参考

      • https://blog.csdn.net/yanbingquan/article/details/50770911 (opens new window)
      #Linux#Linux基础系列#运维知识库
      上次更新: 2023/03/19, 15:09:33
      最近更新
      01
      chromebox/chromebook 刷bios步骤
      03-01
      02
      redis 集群介绍
      11-28
      03
      go语法题二
      10-09
      更多文章>
      Theme by Vdoing | Copyright © 2015-2024 DeanWu | 遵循CC 4.0 BY-SA版权协议
      • 跟随系统
      • 浅色模式
      • 深色模式
      • 阅读模式