jq 命令

作者: 偷油考拉 | 来源:发表于2021-11-25 16:12 被阅读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建构于两种结构:

  • “名称/值”对的集合(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"
}

相关文章

  • jq 命令

    一、JSON 是什么?[https://www.json.org/json-zh.html] JSON(JavaS...

  • jq 命令介绍

    jq 命令介绍 简介 jq 是一款命令行下处理 JSON 数据的工具。其可以接受标准输入,命令管道或者文件中的 J...

  • 用jq解析json

    1.Mac下安装jq 2.命令行查看jq帮助 3.jq官方文档网址:http://stedolan.github....

  • shell调用kylo API

    参考:curl命令详解 yum install -y jq

  • linux jq 命令

    jq 命令可以方便快捷的解析 json 格式的数据。下面几个例子简单的记录了 jq 命令的用法。其中 task_f...

  • Linux命令jq

    是什么 jq 是一个轻量级的json处理命令。可以对json数据进行分片、过滤、映射和转换 jq . 对json数...

  • 读取HDFS中的 Json文件 并排序去重求个数

    命令 : 备注: hdfs dfs -cat 是HDFS文件系统里的cat命令 jq 是linux下的 Json解...

  • 给力的linux命令--jq简易教程

    jq简介 jq可以对json数据进行分片、过滤、映射和转换,和sed、awk、grep等命令一样,都可以让你轻松地...

  • JSON解析利器jq安装

    安装jq Ubuntu可以直接用apt安装 mac下安装 centos好像没有直接安装的命令依次运行以下命令 使用...

  • jq初始,选择器,事件,内容操作,样式操作

    jq初始 jq选择器 jq事件 jq内容操作 jq样式操作

网友评论

      本文标题:jq 命令

      本文链接:https://www.haomeiwen.com/subject/sonatrtx.html