Illustrated Jq Tutorial

栏目: IT技术 · 发布时间: 6年前

Illustrated jq tutorial

The concept of pipes

Unix pipelines were invented in 1973 by Douglas McIlroy as a novel way of stringing together programs, where the output of one program is the input of the next one; It's a way of creating a new program out of combining basic building blocks, McIlroy describes it by analogy as 'screwing together data streams like a garden hose' . This approach quickly became the UNIX philosophy of programming described by McIlroy as follows: 'Write programs that do one thing and do it well. Write programs to work together. Write programs that handle text streams, because that is a universal interface.' Lets say you want to know what are the most common words occurring within a text, the following pipeline will order the words of a text by frequency of usage:
<a href="http://man7.org/linux/man-pages/man1/cat.1.html">cat</a> README.md  | <a href="http://man7.org/linux/man-pages/man1/tr.1.html">tr</a> " " "\n" | <a href="http://man7.org/linux/man-pages/man1/tr.1.html">tr</a>  -d '[:punct:]'  | <a href="http://man7.org/linux/man-pages/man1/sort.1.html">sort</a>   | <a href="http://man7.org/linux/man-pages/man1/uniq.1.html">uniq</a>  -c | sort -n -k 1
This example is a bit like functional programming: in each step of the pipeline the output depends only on the input received via the preceding pipe, each step acts on that text input only and produces its output without writing any files, that is without side effects.

jq - a tool for manipulating structured data

jq is a very versatile tool for working with structured information in json format, the command syntax of jq is also structured by means of a processing pipeline, similar to that of a unix shell, again each processing step acts as a filter/modifier of the input received from the preceding stage. Again on might look at each of these stages as functions in a functional program. This tutorial tries to explain jq in terms of example pipelines; each example comes with links that show you the intermediate results for each stage of the processing pipeline; i think this makes it easier to understand each of the building blocks involved. You can click either on any one of the commands to show the command and how it transforms the input json structure into the output json, each pipe symbol is also a link that will show you the information that flows through it. The html for this tutorial is generated by this script

The tutorial

Get a single scalar values

cat s1.json | jq ' .spec.replicas '

Get a single scalar values (different form, as a pipeline)

cat s1.json | jq ' .spec | .replicas '

Get two scalar values

cat s1.json | jq ' .spec.replicas, .kind '

Get two scalar values and concatenate/format them into a single string

cat s1.json | jq ' "replicas: " + (.spec.replicas | tostring) + " kind: " + .kind '

Select an object from an array of object based on one of the names

cat dep.json | jq ' .status.conditions | map(select(.type == "Progressing")) '

Select a single key value pair from a json object

cat ann.json | jq ' .metadata.annotations | to_entries | map(select(.key == "label1")) | from_entries '

Select two key value pairs from a json object

cat ann.json | jq ' .metadata.annotations | to_entries | map(select(.key == "label1" or .key == "label2")) | from_entries '

Select two key value pairs from a json object (second version)

cat ann.json | jq ' .metadata.annotations | to_entries | map(select(.key == ("label1", "label2"))) | from_entries '

Select all key value pairs from a json object where the name contains substring "label"

cat ann.json | jq ' .metadata.annotations | to_entries | map(select(.key | contains("label"))) | from_entries '

Select all key value pairs from a json object where the name matches the regular expression label[1-9]

cat ann.json | jq ' .metadata.annotations | to_entries | map(select(.key | test("label[1-9]"))) | from_entries '

Add another key value pair to a json object

cat ann.json | jq ' .metadata.annotations += { "label4" : "two" } '

Set all values in a json object

cat ann.json | jq ' .metadata.annotations | to_entries | map_values(.value="override-value") | from_entries '


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

蚂蚁金服

蚂蚁金服

廉薇、边慧、苏向辉、曹鹏程 / 中国人民大学出版社 / 2017-7 / 59.00

打开支付宝,我们不但可以用手机即时付款,给好友转账,为信用卡还款,购买水、电、天然气,还可以办理出国购物退税;因为余额宝,我们可以开始打理手中的零用钱,随时随地进行理财;因为芝麻信用,我们感受到信用为我们带来的信任、尊严与方便——免押金租车、租房、骑行。从支付宝到蚂蚁金服,可以毫不夸张地说,一家企业改变了我们的生活。 蚂蚁金服无疑是目前中国最具代表性的金融科技巨头,同时也是全球估值最高的金融......一起来看看 《蚂蚁金服》 这本书的介绍吧!

Base64 编码/解码
Base64 编码/解码

Base64 编码/解码

html转js在线工具
html转js在线工具

html转js在线工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具