updated jq (#107)

pull/108/head
Igor Chubin 4 years ago
parent 6c7db17f2b
commit da420eb6e4

@ -20,72 +20,134 @@ jq .[0].key_name
# Output the value of a given key of each element in a JSON file: # Output the value of a given key of each element in a JSON file:
jq 'map(.key_name)' jq 'map(.key_name)'
# #
# [ { foo: 1 }, { foo: 2 } ] => [1, 2] # [ { foo: 1 }, { foo: 2 } ]
# => [1, 2]
# Extract as stream of values instead of a list # Extract as stream of values instead of a list
jq '.[] | .foo' jq '.[] | .foo'
# #
# [ { "foo": 1 }, { "foo": 2 } ] => 1, 2 # [ { "foo": 1 }, { "foo": 2 } ]
# => 1, 2
# Slicing # Slicing
jq '.[1:2]' jq '.[1:2]'
# #
# [ { "foo": 1 }, { "foo": 2 } ] => { "foo": 2 } # [ { "foo": 1 }, { "foo": 2 } ]
# => { "foo": 2 }
# Dictionary subset shorthand # Dictionary subset shorthand
jq 'map({ a, b })' jq 'map({ a, b })'
# #
# [ { "a": 1, "b": 2, "c": 3 }, ...] => [ { "a": 1, "b": 2 }, ...] # [ { "a": 1, "b": 2, "c": 3 }, ...]
# => [ { "a": 1, "b": 2 }, ...]
# Parsing json # Converting arbitrary data to json
jq 'with_entries(.value |= fromjson)' --sort-keys jq -r '(map(keys) | add | unique | sort) as $cols | .[] as $row | $cols | map($row[.]) | @csv'
# #
# { "b": "{}", "a": "{}" } # [ { "foo": 1, "bar": 2}, { "foo": 3, "baz": 4}]
# => { "a": {}, "b": {} } #
# => 2,,1
# ,4,3
# Serializing json # Filter a list of objects
jq 'map(select(.name == "foo"))'
# #
jq 'with_entries(.value |= tojson)' --sort-keys # [ { "name": "foo" }, { "name": "bar" } ]
# => [ { "name": "foo" } ]
# #
# { "a": {}, "b": {} } # ## mapping and transforming ##
# => { "a": "{}", "b": "{}" } #
# Add + 1 to all items
jq 'map(.+1)'
# Delete 2 items
jq 'del(.[1, 2])'
# Concatenate arrays
jq 'add'
# Flattening json # Flatten an array
jq 'flatten(1)' jq 'flatten'
# #
# [[1], [2]] # [[1], [2]]
# => [1, 2] # => [1, 2]
# Converting to csv # Create a range of numbers
jq '.[] | [.foo, .bar] | @csv' -r jq '[range(2;4)]'
#
# [{ "foo": 1, "bar": 2, "baz":3 }] # Display the type of each item
# => 1,2 jq 'map(type)'
# Sort # Sort an array of basic type
jq 'sort' jq 'sort'
#
# [3, 2, 1] # [3, 2, 1]
# => [1, 2, 3] # => [1, 2, 3]
# Deleting duplicates (dedup / uniq) # Sort an array of objects
jq unique jq 'sort_by(.foo)'
# Sort lines of a file
jq --slurp '. | sort | .[]'
# 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)'
# #
# [1, 1, 2, 1] # [1, 1, 2, 1]
# => [1, 2] # => [1, 2]
# Sort lines of a file # Reverse an array
jq --slurp '. | sort | .[]' jq 'reverse'
# Converting arbitrary data to json
jq -r '(map(keys) | add | unique | sort) as $cols | .[] as $row | $cols | map($row[.]) | @csv'
# [ { "foo": 1, "bar": 2}, { "foo": 3, "baz": 4}]
# #
# => 2,,1 # ## jq in shell scripts ##
# ,4,3 #
# 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)"')
# Filter a list of objects
jq 'map(select(.name == "foo"))'
# #
# [ { "name": "foo" }, { "name": "bar" } ] # ## Input/output formats ##
# => [ { "name": "foo" } ] #
# 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
#
# [{ "foo": 1, "bar": 2, "baz":3 }]
# => 1,2

Loading…
Cancel
Save