brief introduction
In the daily development under linux, we often need to process and analyze the json format data. jq Is an open source JSON format data processing tool, it provides a lot of flexible syntax, very powerful.
install
- macOS:
brew install jq
- Ubuntu
apt-get install jq
- Other reference Official website
jq syntax
Basic usage
Suppose that the text test ABCD JSON has the following string
{ "name":"xiaoming", "age":21, "birthday":"10th August", "email":"yj.xxxx@xxxx.com", "skills":[ { "name":"C++", "level":2 }, { "name":"Python", "level":1 } ] }
Filter by key
jq expression
cat test_json | jq ".name"
output
"xiaoming"
Filter by index
jq expression
cat test_json | jq ".skills[0]"
output
{ "name": "C++", "level": 2 }
Traversing array
jq expression
cat test_json | jq ".skills[]"
output
{ "name": "C++", "level": 2 } { "name": "Python", "level": 1 }
Filter by Pipe ("|)
jq expression
cat test_json | jq ".skills[] | .level"
output
2 1
Construct Array
jq expression
cat test_json | jq "[.age, .email]"
output
[ 21, "yj.xxxx@xxxx.com" ]
Construct Mapping
jq expression
cat test_json | jq "{name, skills_2 : .skills}"
output
{ "name": "xiaoming", "skills_2": [ { "name": "C++", "level": 2 }, { "name": "Python", "level": 1 } ] }
jq expression
cat test_json | jq "{(.name): .skills}"
output
{ "xiaoming": [ { "name": "C++", "level": 2 }, { "name": "Python", "level": 1 } ] }
Built in operators and functions
addition
jq expression
cat test_json | jq ".age + 1"
output
22
Character mosaic
cat test_json | jq ".age + .name"
output
"yj.xxxx@xxxx.comxiaoming"
Subtraction -
jq expression
echo '["xml", "yaml", "json"]' | jq '. - ["xml", "yaml"]'
output
[ "json" ]
Length
jq expression
echo '[[1,2], "string", {"a":2}, null]' | jq '[.[]| length]'
output
[ 2, 6, 1, 0 ]
keys
jq expression
cat test_json | jq 'keys'
output
[ "age", "birthday", "email", "name", "skills" ]
in
jq expression
echo '["foo", "bar"]' | jq '.[] | in({"foo": 42})'
output
true false
map
jq expression
echo '[1,2,3]' | jq 'map(.+1)'
output
[ 2, 3, 4 ]
map_values
jq expression
echo '{"a": 1, "b": 2, "c": 3}' | jq 'map_values(.+1)'
output
{ "a": 2, "b": 3, "c": 4 }
del
js expression
echo '{"foo": 42, "bar": 9001, "baz": 42}' | jq "del(.foo)"
output
{ "bar": 9001, "baz": 42 }
select
jq expression
echo '[{"id": "first", "val": 1}, {"id": "second", "val": 2}]' | jq '.[] | select(.id == "second")'
output
{ "id": "second", "val": 2 }
any
jq expression
echo '[true, false]' | jq 'any'
output
true
all
jq expression
echo '[true, false]' | jq 'all'
output
false
min,max
jq expression
echo '[5,4,2,7]' | jq 'min'
output
2
sort,sort_by
jq expression
echo '[8,3,null,6]' | jq 'sort'
output
[ null, 3, 6, 8 ]
jq expression
echo '[{"foo":4, "bar":10}, {"foo":3, "bar":100}, {"foo":2, "bar":1}]' | jq 'sort_by(.foo)'
output
[ { "foo": 2, "bar": 1 }, { "foo": 3, "bar": 100 }, { "foo": 4, "bar": 10 } ]
index
jq expression
echo '["a","b", "c"]' | jq 'index("b")'
output
1
json
jq expression
echo '["a","b","c"]' | jq 'join(";")'
output
"a;b;c"
Conditional judgement
jq expression
echo 2 | jq 'if . == 0 then "zero" elif . == 1 then "one" else "many" end'
output
"many"