jq 命令

一、JSON 是什么?

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。 易于人阅读和编写。同时也易于机器解析和生成。 它基于JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一个子集。 JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使JSON成为理想的数据交换语言。

JSON建构于两种结构:

  • “名称/值”对的集合(A collection of name/value pairs)。
    不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。
  • 值的有序列表(An ordered list of values)。
    在大部分语言中,它被理解为数组(array)。

这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。

范例:

{
  "jsonapi": { "version": "1.0" },
  "errors": [
    {
      "code":   "123",
      "source": { "pointer": "/data/attributes/firstName" },
      "title":  "Value is too short",
      "detail": "First name must contain at least three characters."
    },
    {
      "code":   "225",
      "source": { "pointer": "/data/attributes/password" },
      "title": "Passwords must contain a letter, number, and punctuation character.",
      "detail": "The password provided is missing a punctuation character."
    },
    {
      "code":   "226",
      "source": { "pointer": "/data/attributes/password" },
      "title": "Password and password confirmation do not match."
    }
  ]
}

二、jq 是什么?

  1. 格式化json输出
    cat testjson |jq or cat testjson |jq '.'
{
  "jsonapi": {
    "version": "1.0"
  },
  "errors": [
    {
      "code": "123",
      "source": {
        "pointer": "/data/attributes/firstName"
      },
      "title": "Value is too short",
      "detail": "First name must contain at least three characters."
    },
    {
      "code": "225",
      "source": {
        "pointer": "/data/attributes/password"
      },
      "title": "Passwords must contain a letter, number, and punctuation character.",
      "detail": "The password provided is missing a punctuation character."
    },
    {
      "code": "226",
      "source": {
        "pointer": "/data/attributes/password"
      },
      "title": "Password and password confirmation do not match."
    }
  ]
}
  1. 打印某name
    cat testjson |jq '.jsonapi'
{
  "version": "1.0"
}

cat testjson |jq '.errors'

[
  {
    "code": "123",
    "source": {
      "pointer": "/data/attributes/firstName"
    },
    "title": "Value is too short",
    "detail": "First name must contain at least three characters."
  },
  {
    "code": "225",
    "source": {
      "pointer": "/data/attributes/password"
    },
    "title": "Passwords must contain a letter, number, and punctuation character.",
    "detail": "The password provided is missing a punctuation character."
  },
  {
    "code": "226",
    "source": {
      "pointer": "/data/attributes/password"
    },
    "title": "Password and password confirmation do not match."
  }
]
  1. 打印数组内的某个元素
    cat testjson |jq '.errors[0]'
{
  "code": "123",
  "source": {
    "pointer": "/data/attributes/firstName"
  },
  "title": "Value is too short",
  "detail": "First name must contain at least three characters."
}
  1. 打印数组下,全部元素下的某个name:key
    cat testjson |jq '.errors[] |{code: .code ,title: .title ,pointer: .source.pointer}'
{
  "code": "123",
  "title": "Value is too short",
  "pointer": "/data/attributes/firstName"
}
{
  "code": "225",
  "title": "Passwords must contain a letter, number, and punctuation character.",
  "pointer": "/data/attributes/password"
}
{
  "code": "226",
  "title": "Password and password confirmation do not match.",
  "pointer": "/data/attributes/password"
}
  1. 打印全部的keys
    cat testjson |jq 'keys'
[sysadmin@VM-201-4-centos ~]$ cat 111 |jq 'keys'
[
  "connectionCheckUrl",
  "core",
  "deprecations",
  "generationTimestamp",
  "id",
  "plugins",
  "signature",
  "updateCenterVersion",
  "warnings"
]
[sysadmin@VM-201-4-centos ~]$ cat 111 |jq '.core'
{
  "buildDate": "May 31, 2023",
  "name": "core",
  "sha1": "5x/NLAMG1bqoOpuGKo8xP4lrDZw=",
  "sha256": "YAtz6r95eFLjmRlUG4T3aG/2Abl8d7ROsAhD65HH3Ww=",
  "size": 98362423,
  "url": "https://updates.jenkins.io/download/war/2.401.1/jenkins.war",
  "version": "2.401.1"
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • jq 命令介绍 简介 jq 是一款命令行下处理 JSON 数据的工具。其可以接受标准输入,命令管道或者文件中的 J...
    mini鱼阅读 5,763评论 0 2
  • jq可以对json数据进行分片、过滤、映射和转换 安装 提取信息 格式化展示原文 输出数组中的元素,可以使用[in...
    十毛tenmao阅读 419评论 1 3
  • 格式化 json 数据 有时候,我们在命令行请求 Restful 风格的接口,返回的 json 响应是连在一起,如...
    riverlcn阅读 2,593评论 0 0
  • 一、MemCache简介 session MemCache是一个自由、源码开放、高性能、分布式的分布式内存对象缓存...
    李伟铭MIng阅读 3,873评论 2 13
  • client,page和screen的区别? clientX,clientY是触摸点相对于viewport视口x,...
    change_22fa阅读 1,724评论 1 1