jq 命令

2021-11-25  本文已影响0人  偷油考拉

一、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建构于两种结构:

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

范例:

{
  "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"
}
上一篇 下一篇

猜你喜欢

热点阅读