JQ Commands

Published: 2017-10-28, Updated: 2021-05-23

Links

Json para CSV

$ echo '[{"name": "Elvis", "age": 25}, {"name": "Bruna", "age": 22}]' | jq -r '.[] | [.name, .age] | @csv'
"\"Elvis\",25"
"\"Bruna\",22"

Jogar resultados em um só array

$ echo '[{"name": "Maria"}, {"name": "John"}]' | jq '.[]'
{
  "name": "Maria"
}
{
  "name": "John"
}
$ echo '[{"name": "Maria"}, {"name": "John"}]' | jq '.[]' | jq -s '.'
[
  {
    "name": "Maria"
  },
  {
    "name": "John"
  }
]

Iterar em items que podem ser nulos

 cat comentarios-canal-2021-05-22.json  | jq '.[].items | .[]?.snippet.topLevelComment.snippet' | less

Print array of objects fields

skins.json

[{
 "hash": "Glock-18 | Water Elemental (Minimal Wear)",
 "price": "4.4700",
 "float": "0.11328461",
 "sold": "2019-03-09T09:40:26-03:00"
},
{
 "hash": "Glock-18 | Water Elemental (Minimal Wear)",
 "price": "4.1100",
 "float": "0.12572628",
 "sold": "2019-03-09T08:17:49-03:00"
}]
cat skins.json | jq -r '.[] | "\(.price), \(.sold)"'

Mapping array object to another array objects

echo '[{"foo":10, "bar":200}]' | jq '.[] | {age: .foo}'

Get object keys

echo '{ "host" : "1",    "user" : "root",    "private_key" : "x"  }' | jq 'keys'
[
  "host",
  "private_key",
  "user"
]

Get object keys as array and count

echo '{ "host" : "1",    "user" : "root",    "private_key" : "x"  }' | jq 'keys|length'
3

Get object keys then get the 2 first items from the resulted key array

echo '{ "host" : "1",    "user" : "root",    "private_key" : "x"  }' | jq 'keys|.[0:2]'
[
  "host",
  "private_key"
]

property substring

echo '{"name": "Elvis"}' | jq '.name[2:]'
vis

Filter through an array of object

$ json='[{"genre":"deep house"}, {"genre": "progressive house"}, {"genre": "dubstep"}]' &&\
echo "$json" | jq '.[] | select(.genre | contains("house"))'
{"genre":"deep house"}
{"genre":"progressive house"}

# or
$ echo "$json" | jq '[ .[] | select( .genre | contains("house")) ]'
[{"genre":"deep house"},{"genre":"progressive house"}]

# or
$ echo "$json" | jq 'map(select(.genre | contains("house")))'
[{"genre":"deep house"},{"genre":"progressive house"}]

Extract value without quotes eg. (JSON inside string property)

echo '{"name":"some-name" ,"value":"[{\"hashName\":\"★ Bayonet\"}]","timestamp":1531335404,"status":200}' | jq -r '.value' | jq '.'
echo '{"value": "{\"name\": 12}" }' | python -c 'import sys, json; print json.load(sys.stdin)["value"]' | python -m json.tool

Extrair json de dentro de uma propriedade

$ echo '[
{  "title": "My Title 2", "metadata": "{\"name\": \"Grape\"}" },
{  "title": "My Title", "metadata": "{\"name\": \"Orange\"}" }
]' | jq '.[] | { "title": .title, metadata: .metadata | fromjson }'
{
  "title": "My Title 2",
  "metadata": {
    "name": "Grape"
  }
}
{
  "title": "My Title",
  "metadata": {
    "name": "Orange"
  }
}

Passando variáveis do bash para o JQ


export APP_VERSION=1.0
export DESC='some "desc'
export CURRENT_BRANCH=master

echo '{}' | jq --arg tag_name "$APP_VERSION" \
--arg target_commitish "$CURRENT_BRANCH" \
--arg body "$DESC" '{
  $tag_name,
  $target_commitish,
  name: $tag_name,
  $body,
  "draft": false,
  "prerelease": true
}'

Keywords

How to use jq


Arquiteturas de Microservices GTA San Andreas Bookmarks

Comments