jq命令的使用

考虑到K8S经常用到jsonpath,他自带的语法不太强,所以还是用jq做处理更方便些。记录下常用的语法。

k get pod | 
  jq -r -c '..|.containers?|values|.[] |{name, path: .volumeMounts[]} | .name + " " + .path.mountPath '

演练数据

hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'
{
  "num": 1,
  "str": "string demo",
  "array": [
    1,
    2,
    3
  ],
  "object": {
    "a": 1,
    "b": 2
  },
  "array_object": [
    {
      "x": 1,
      "y": 2
    },
    {
      "x": 3,
      "y": 4
    }
  ]
}

基本用法

#基本数据取值
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq '.num'
1
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq '.str'
"string demo"

#数组取值
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq  '.array'
[
  1,
  2,
  3
]

#对象输出
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq  '.object'
{
  "a": 1,
  "b": 2
}
#对象紧凑输出
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq -c '.object'
{"a":1,"b":2}

#递归寻找
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq '..|.a?'
null
1
null
null
#递归寻找,去掉null值
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq '..|.a?|values'
1

#输出对象
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq '{str, array}'
{
  "str": "string demo",
  "array": [
    1,
    2,
    3
  ]
}

#数组扁平化
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq '{str, array_item: .array[]}'
{
  "str": "string demo",
  "array_item": 1
}
{
  "str": "string demo",
  "array_item": 2
}
{
  "str": "string demo",
  "array_item": 3
}

#数组扁平化结合对象输出
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq '{str, array_item: .array_object[]}|{str, x: .array_item.x}'
{
  "str": "string demo",
  "x": 1
}
{
  "str": "string demo",
  "x": 3
}
#紧凑输出
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq '{str, array_item: .array_object[]}|{str, x: .array_item.x}' -c
{"str":"string demo","x":1}
{"str":"string demo","x":3}

#输出为shell方便读的一行格式
hogwarts: ~ seveniruby$ jq -n '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq '{str, array_item: .array_object[]}|{str, x: .array_item.x} | [.str, .x]|join(",")' -cr
string demo,1
string demo,3

输入输出处理

#用来做转义字符串解析
hogwarts: ~ seveniruby$ echo ss'ddd"xxx"d\"dd' | jq -R
"ssddd\"xxx\"d\\\"dd"

#等价做法 使用-n与--arg
jq -n --arg a  ss'ddd"xxx"d\"dd' '$a'
"ssddd\"xxx\"d\\\"dd"

#拼凑ElasticSearch的SQL查询命令
jq -n --arg a 'select * "from ceba-collect-*" where userid="ceshiren.com"' '{query: $a}'
{
  "query": "select * \"from ceba-collect-*\" where userid=\"ceshiren.com\""
}

#raw输出,去掉双引号
hogwarts: ~ seveniruby$ echo 1 | jq '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq -r '.str'
string demo

#紧凑输出
hogwarts: ~ seveniruby$ echo 1 | jq '{num: 1, str: "string demo", array: [1,2,3], object: {a: 1, b: 2}, array_object: [{x: 1, y: 2}, {x: 3, y: 4}]}'  | jq -c '.array'
[1,2,3]

参数列表

jq - commandline JSON processor [version 1.6]

Usage:	jq [options] <jq filter> [file...]
	jq [options] --args <jq filter> [strings...]
	jq [options] --jsonargs <jq filter> [JSON_TEXTS...]

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 copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).

For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq

Example:

	$ echo '{"foo": 0}' | jq .
	{
		"foo": 0
	}

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>;
  --rawfile a f    set variable $a to a string consisting of the contents of <f>;
  --args           remaining arguments are string arguments, not files;
  --jsonargs       remaining arguments are JSON arguments, not files;
  --               terminates argument processing;

Named arguments are also available as $ARGS.named[], while
positional arguments are available as $ARGS.positional[].

See the manpage for more options.

文档

参考 man jq

https://stedolan.github.io/jq/manual/