Skip to content

Commit

Permalink
updated jq (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
chubin committed May 21, 2020
1 parent 6c7db17 commit da420eb
Showing 1 changed file with 100 additions and 38 deletions.
138 changes: 100 additions & 38 deletions sheets/jq
Original file line number Diff line number Diff line change
Expand Up @@ -20,72 +20,134 @@ jq .[0].key_name
# Output the value of a given key of each element in a JSON file:
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
jq '.[] | .foo'
#
# [ { "foo": 1 }, { "foo": 2 } ] => 1, 2
# [ { "foo": 1 }, { "foo": 2 } ]
# => 1, 2

# Slicing
jq '.[1:2]'
#
# [ { "foo": 1 }, { "foo": 2 } ] => { "foo": 2 }
# [ { "foo": 1 }, { "foo": 2 } ]
# => { "foo": 2 }

# Dictionary subset shorthand
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
jq 'with_entries(.value |= fromjson)' --sort-keys
# Converting arbitrary data to json
jq -r '(map(keys) | add | unique | sort) as $cols | .[] as $row | $cols | map($row[.]) | @csv'
#
# { "b": "{}", "a": "{}" }
# => { "a": {}, "b": {} }

# Serializing json
# [ { "foo": 1, "bar": 2}, { "foo": 3, "baz": 4}]
#
jq 'with_entries(.value |= tojson)' --sort-keys
# => 2,,1
# ,4,3

# Filter a list of objects
jq 'map(select(.name == "foo"))'
#
# { "a": {}, "b": {} }
# => { "a": "{}", "b": "{}" }
# [ { "name": "foo" }, { "name": "bar" } ]
# => [ { "name": "foo" } ]

# Flattening json
jq 'flatten(1)'
#
# [[1], [2]]
# => [1, 2]
# ## mapping and transforming ##
#

# Converting to csv
jq '.[] | [.foo, .bar] | @csv' -r
# Add + 1 to all items
jq 'map(.+1)'

# Delete 2 items
jq 'del(.[1, 2])'

# Concatenate arrays
jq 'add'

# Flatten an array
jq 'flatten'
#
# [{ "foo": 1, "bar": 2, "baz":3 }]
# => 1,2
# [[1], [2]]
# => [1, 2]

# Sort
jq 'sort'
# [3, 2, 1]
# => [1, 2, 3]
# Create a range of numbers
jq '[range(2;4)]'

# Display the type of each item
jq 'map(type)'

# Deleting duplicates (dedup / uniq)
jq unique
# Sort an array of basic type
jq 'sort'
#
# [1, 1, 2, 1]
# => [1, 2]
# [3, 2, 1]
# => [1, 2, 3]

# Sort an array of objects
jq 'sort_by(.foo)'

# Sort lines of a file
jq --slurp '. | sort | .[]'

# 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}]
# 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)'
#
# => 2,,1
# ,4,3
# [1, 1, 2, 1]
# => [1, 2]

# Filter a list of objects
jq 'map(select(.name == "foo"))'
# Reverse an array
jq 'reverse'

#
# ## 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)"')


#
# ## 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
#
# [ { "name": "foo" }, { "name": "bar" } ]
# => [ { "name": "foo" } ]
# [{ "foo": 1, "bar": 2, "baz":3 }]
# => 1,2

0 comments on commit da420eb

Please sign in to comment.