2
0
mirror of https://github.com/chubin/cheat.sheets synced 2024-11-19 03:25:44 +00:00
cheat.sheets/sheets/jq

155 lines
2.8 KiB
Plaintext
Raw Normal View History

2017-07-02 11:09:28 +00:00
# jq
# A lightweight and flexible command-line JSON processor.
# Output a JSON file, in pretty-print format:
jq
# Output all elements from arrays
# (or all key-value pairs from objects) in a JSON file:
jq .[]
# Read JSON objects from a file into an array, and output it
# (inverse of jq .[]):
2017-07-02 11:09:28 +00:00
jq --slurp
# Output the first element in a JSON file:
jq .[0]
# Output the value of a given key of the first element in a JSON file:
jq .[0].key_name
# Output the value of a given key of each element in a JSON file:
jq 'map(.key_name)'
#
2020-05-21 11:05:58 +00:00
# [ { foo: 1 }, { foo: 2 } ]
# => [1, 2]
2017-07-02 11:09:28 +00:00
# Extract as stream of values instead of a list
jq '.[] | .foo'
#
2020-05-21 11:05:58 +00:00
# [ { "foo": 1 }, { "foo": 2 } ]
# => 1, 2
2017-07-02 11:09:28 +00:00
# Slicing
jq '.[1:2]'
#
2020-05-21 11:05:58 +00:00
# [ { "foo": 1 }, { "foo": 2 } ]
# => { "foo": 2 }
2017-07-02 11:09:28 +00:00
# Dictionary subset shorthand
jq 'map({ a, b })'
#
2020-05-21 11:05:58 +00:00
# [ { "a": 1, "b": 2, "c": 3 }, ...]
# => [ { "a": 1, "b": 2 }, ...]
2017-07-02 11:09:28 +00:00
2020-05-21 11:05:58 +00:00
# Converting arbitrary data to json
jq -r '(map(keys) | add | unique | sort) as $cols \
| .[] as $row | $cols | map($row[.]) | @csv'
2017-07-02 11:09:28 +00:00
#
2020-05-21 11:05:58 +00:00
# [ { "foo": 1, "bar": 2}, { "foo": 3, "baz": 4}]
2017-07-02 11:09:28 +00:00
#
2020-05-21 11:05:58 +00:00
# => 2,,1
# ,4,3
# Filter a list of objects
jq 'map(select(.name == "foo"))'
2017-07-02 11:09:28 +00:00
#
2020-05-21 11:05:58 +00:00
# [ { "name": "foo" }, { "name": "bar" } ]
# => [ { "name": "foo" } ]
2017-07-02 11:09:28 +00:00
#
2020-05-21 11:05:58 +00:00
# ## mapping and transforming ##
#
2017-07-02 11:09:28 +00:00
2020-05-21 11:05:58 +00:00
# Add + 1 to all items
jq 'map(.+1)'
# Delete 2 items
jq 'del(.[1, 2])'
# Concatenate arrays
jq 'add'
# Flatten an array
jq 'flatten'
#
2020-05-21 11:05:58 +00:00
# [[1], [2]]
# => [1, 2]
2017-07-02 11:09:28 +00:00
2020-05-21 11:05:58 +00:00
# Create a range of numbers
jq '[range(2;4)]'
# Display the type of each item
jq 'map(type)'
2017-07-02 11:09:28 +00:00
2020-05-21 11:05:58 +00:00
# Sort an array of basic type
jq 'sort'
2017-07-02 11:09:28 +00:00
#
2020-05-21 11:05:58 +00:00
# [3, 2, 1]
# => [1, 2, 3]
# Sort an array of objects
jq 'sort_by(.foo)'
2017-07-02 11:09:28 +00:00
# Sort lines of a file
jq --slurp '. | sort | .[]'
2020-05-21 11:05:58 +00:00
# Group by a key - opposite to flatten
jq 'group_by(.foo)'
# Minimun value of an array
jq 'min'
# See also min, max, min_by(path_exp), max_by(path_exp)
# Remove duplicates
jq 'unique'
# or
jq 'unique_by(.foo)'
# or
jq 'unique_by(length)'
2017-07-02 11:09:28 +00:00
#
2020-05-21 11:05:58 +00:00
# [1, 1, 2, 1]
# => [1, 2]
2020-02-08 12:26:52 +00:00
2020-05-21 11:05:58 +00:00
# Reverse an array
jq 'reverse'
#
2020-05-21 11:05:58 +00:00
# ## jq in shell scripts ##
#
# URL Encode something
date | jq -sRr @uri
# Thu%2021%20May%202020%2012%3A40%3A40%20PM%20CEST%0A
# To create proper JSON from a shell script and properly escape variables:
jq -n --arg foobaz "$FOOBAZ" '{"foobaz":$foobaz}'
# To fill environment variables from JSON object keys
# (e.g. $FOO from jq query ".foo")
export $(jq -r '@sh "FOO=\(.foo) BAZ=\(.baz)"')
#
2020-05-21 11:05:58 +00:00
# ## Input/output formats ##
#
# Parsing json
jq 'with_entries(.value |= fromjson)' --sort-keys
#
# { "b": "{}", "a": "{}" }
# => { "a": {}, "b": {} }
# Serializing json
#
jq 'with_entries(.value |= tojson)' --sort-keys
#
# { "a": {}, "b": {} }
# => { "a": "{}", "b": "{}" }
# Converting to csv
jq '.[] | [.foo, .bar] | @csv' -r
2020-02-08 12:26:52 +00:00
#
2020-05-21 11:05:58 +00:00
# [{ "foo": 1, "bar": 2, "baz":3 }]
# => 1,2