🖥️jq

➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the jq command with important options and switches using examples.

▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁

#                     ██╗ ██████╗ 
#                     ██║██╔═══██╗
#                     ██║██║   ██║
#                ██   ██║██║▄▄ ██║
#                ╚█████╔╝╚██████╔╝
#                 ╚════╝  ╚══▀▀═╝ 

# 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 .[]):
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)'
#
# [ { foo: 1 }, { foo: 2 } ] => [1, 2]

# Extract as stream of values instead of a list
jq '.[] | .foo'
#
# [ { "foo": 1 }, { "foo": 2 } ] => 1, 2

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

# Dictionary subset shorthand
jq 'map({ a, b })'
#
# [ { "a": 1, "b": 2, "c": 3 }, ...] => [ { "a": 1, "b": 2 }, ...]

# 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": "{}" }

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

# Converting to csv
jq '.[] | [.foo, .bar] | @csv' -r
# 
# [{ "foo": 1, "bar": 2, "baz":3 }]
# => 1,2

# Sort
jq 'sort'
# [3, 2, 1]
# => [1, 2, 3]

# Deleting duplicates (dedup / uniq)
jq unique
#
# [1, 1, 2, 1]
# => [1, 2]

# 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}]
#
# => 2,,1
#    ,4,3
                                
										  
# Pretty print the json
jq "." < filename.json

# Access the value at key "foo"
jq '.foo'

# Access first list item
jq '.[0]'

# Slice & Dice
jq '.[2:4]'
jq '.[:3]'
jq '.[-2:]'

cat file.json | jq
# use jq to validate and pretty-print json output
# the `jq` tool can also be used do validate json files and pretty print output `cat file.json | jq` available on several platforms, including newer debian-based systems via `#sudo apt install jq`, mac via `brew install jq`, and from source https://stedolan.github.io/jq/download/ This is sample output - yours may be different.
success:
{
  "foo": "bar"
}

error:
$ echo '{"foo":{"bar":}}'| jq
parse error: Unmatched '}' at line 1, column 15

diff <(jq . -M -S < old.json) <(jq . -M -S < new.json)
# Get a diff of two json arrays - jq is amazing for manipulating json on the commandline, but the developers have some weird ideas about how to handle shell redirections. This command works around them. Further reading: https://github.com/stedolan/jq/issues/1110

jq < file.json
# Use jq to validate and pretty-print json output
# The `jq` tool can also be used do validate json files and pretty print output: ` jq < file.json` Available on several platforms, including newer debian-based systems via `#sudo apt install jq`, mac via `brew install jq`, and from source https://stedolan.github.io/jq/download/ This alternative to the original avoids the useless use of cat This is sample output - yours may be different.
success:
{
  "foo": "bar"
}

error:
$ jq < file.json
parse error: Unmatched '}' at line 1, column 15

#==============================##==============================#
# CMD JQ						       #
#==============================##==============================#
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░

  █║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌

              ██╗ ██╗ ██████╗  ██████╗ ██╗  ██╗███████╗██████╗
             ████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
             ╚██╔═██╔╝██║  ██║██║   ██║ ╚███╔╝ █████╗  ██║  ██║
             ████████╗██║  ██║██║   ██║ ██╔██╗ ██╔══╝  ██║  ██║
             ╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
              ╚═╝ ╚═╝ ╚═════╝  ╚═════╝ ╚═╝  ╚═╝╚══════╝╚═════╝

               █║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌

░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░