jq

$ echo '{ "name": "Luke Skywalker" }' | jq '.name'
"Luke Skywalker"
$

Let’s extract property value from an object.

$ echo '{ "name": "Luke Skywalker" }' | jq '.name'
"Luke Skywalker"
$

Sometimes we need values without quotation marks. Use -r to switch to raw format.

$ echo '{ "name": "Luke Skywalker" }' | jq -r '.name'
Luke Skywalker
$

Filling luke.json with arbitrary JSON.

$ echo '
{
  "name": "Luke Skywalker",
  "films": [
    "http://swapi.co/api/films/2/",
    "http://swapi.co/api/films/6/",
    "http://swapi.co/api/films/3/",
    "http://swapi.co/api/films/1/",
    "http://swapi.co/api/films/7/",
  ]
}
' > luke.json
$

Extract from the list.

$ jq '.films | .[]' < luke.json
"http://swapi.co/api/films/2/"
"http://swapi.co/api/films/6/"
"http://swapi.co/api/films/3/"
"http://swapi.co/api/films/1/"
"http://swapi.co/api/films/7/"
$

Sort and then reverse.

$ jq '.films | sort | reverse | .[]' < luke.json
"http://swapi.co/api/films/7/"
"http://swapi.co/api/films/6/"
"http://swapi.co/api/films/3/"
"http://swapi.co/api/films/2/"
"http://swapi.co/api/films/1/"
$

Call predicate for every element of the list.

$ jq '.films | sort | reverse | .[] | contains("1")' < luke.json
false
false
false
false
true
$

Keep elements of the list if contains("1") == true.

$ jq '.films | .[] | select(. | contains("1"))' < luke.json
"http://swapi.co/api/films/1/"
$

Or you can use map:

$ jq '.films | map(select(. | contains("1"))) | .[]' < luke.json
"http://swapi.co/api/films/1/"
$

© RGZ.EE OÜ