jq

jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.

jq is written in portable C, and it has zero runtime dependencies. You can download a single binary, scp it to a far away machine of the same type, and expect it to work.

Examples:
# Basic Selection
echo '{"name": "John", "age": 30, "city": "New York"}' | jq '.name'
# Output: "John"

# Filtering Arrays
echo '[{"name": "Alice", "age": 25}, {"name": "Bob", "age": 32}]' | jq '.[] | select(.age > 30) | .name'
# Output: "Bob"

# Conditional Statements
echo '{"name": "Sam", "age": 22}' | jq 'if .age >= 21 then "Can drink" else "Cannot drink" end'
# Output: "Can drink"

# Reading from a File
jq '.users[] | .name' data.json

# Combining Filters
echo '{"people": [{"name": "Alex", "age": 35}, {"name": "Eva", "age": 28}]}' | jq '.people[] | select(.age > 30) | .name'
# Output: "Alex"

# Parsing JSON from a URL
curl -s https://api.example.com/data | jq '.results[0].name'

# Sorting Arrays
echo '[3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5]' | jq 'sort'
# Output: [1,1,2,3,3,4,5,5,5,6,9]

# Formatting Output
echo '{"name": "Tom", "age": 40}' | jq -r '"Name: \(.name), Age: \(.age)"'
# Output: Name: Tom, Age: 40

# Adding a New Field
echo '{"name": "Alice", "age": 25}' | jq '. | .city = "New York"'
# Output: {"name": "Alice", "age": 25, "city": "New York"}

# Changing Field Values
echo '{"name": "Bob", "age": 32}' | jq '. | .age = .age + 5'
# Output: {"name": "Bob", "age": 37}

# Filtering and Mapping
echo '{"people": [{"name": "Charlie", "age": 30}, {"name": "David", "age": 28}]}' | jq '.people[] | {full_name: (.name + " Smith"), doubled_age: (.age * 2)}'
# Output:
# {"full_name": "Charlie Smith", "doubled_age": 60}
# {"full_name": "David Smith", "doubled_age": 56}

# Array Transformation
echo '[1, 2, 3, 4, 5]' | jq 'map(. * 2)'
# Output: [2, 4, 6, 8, 10]

# String Concatenation
echo '{"first_name": "John", "last_name": "Doe"}' | jq '. | .full_name = .first_name + " " + .last_name'
# Output: {"first_name": "John", "last_name": "Doe", "full_name": "John Doe"}

# Conditional Transformation
echo '{"value": 15}' | jq '. | if .value > 10 then .status = "High" else .status = "Low" end'
# Output: {"value": 15, "status": "High"}

jq - commandline JSON processor [version 1.6]

Usage:	jq [options] "jq filter" [file...]
	jq [options] --args "jq filter" [strings...]
	jq [options] --jsonargs "jq filter" [JSON_TEXTS...]

jq is a tool for processing JSON inputs, applying the given filter to
its JSON text inputs and producing the filter's results as JSON on
standard output.

The simplest filter is ., which copies jq's input to its output
unmodified (except for formatting, but note that IEEE754 is used
for number representation internally, with all that that implies).

For more advanced filters see the jq(1) manpage ("man jq")
and/or https://stedolan.github.io/jq

Example:

	$ echo '{"foo": 0}' | jq .
	{
		"foo": 0
	}

For a listing of options, use jq --help