Jq
Утилита для работы с json
Установка
apt install jq
Примеры использования
Вывести список хостов заббикса, хостнейм которых заканчивается на com и сформировать json с ключами host и hostid
jq .result | jq '.[] | select(.host|test(".com$"))' | jq '{host, hostid}'
{ "host": "host1.example.com", "hostid": "10600" } { "host": "host2.example.com", "hostid": "10601" }
Вывести список хостов заббикса, хостнейм которых НЕ заканчивается на com и сформировать json с ключами host и hostid
jq .result | jq '.[] | select(.host|test(".com$")|not)' | jq '{host, hostid}'
{ "host": "host1.example.ru", "hostid": "10602" } { "host": "host2.example.ru", "hostid": "10603" }
Испльзовать несколько фильтров. Отобразить только включенные хосты из прошлого примера
jq .result | jq '.[] | select((.host|test(".com$")|not) and (.status=="0"))' | jq '{host, hostid}'
{ "host": "host1.example.ru", "hostid": "10602" }
Получить sub-key по имени 'data'
$ jq .data file.json
Получить несколько sub-keys
$ jq ".data,.name" file.json
Получить несколько sub-keys в объекте вместе со значениями
jq '{program,message}' - обратите внимание на отсутствие точки
How to "de-array" something?
Place [] at the end of the key
jq .results[]
Get nth item of an array off a sub-key
- 0th item off the .results key
jq '.results | .[0]'
Get deeply nested data
$ jq .data.product_id.value file.json
or with piping (i.e. piping and dots can substitute for one another)
$ jq ".data | .product_id | .value" file.json Key .name value for each item in a (top-level here) array
$ jq ".[] | .name" file.json
(outputs an array of names)
make sure you don't forget to include any intermediate object keys
e.g.
$ jq ".[] | .intermediate.name" file.json Merge entries from two keys
jq '.devDependencies + .dependencies' package.json
Merge entries from multiple files Get count of entities
- Just pipe to length
$ jq '.dependencies, .devDependencies | length' package.json 16 15
NB: notice how multiple top-level keys were used with the comma operator
Filter records that have null at a value
$ jq '.[] | select (.coreTransactionData.productId == null)' file.json
notice use of select (conditional)
(not null is just != instead)
Concat bits together to form one string
Use parenteses and the plus operator
jq '(.program + ":::" + .message)'
List the keys without values
jq '.properties | keys' file.json
Note that 'keys' has no dot in front of it (since it is a built-in command rather than a property of this particular piece of JSON) Warp the output of a jq filter in an array (i.e. to appear as an array of objects and be valid for import)
Transform a command like so:
jq '.[] | select ((.payload.coreTransactionData.productId != null))' paymentConfirmedEvents--FromDeals.json
into this: (i.e. wrap the entire command in the array notation, and the do the piping within this array.)
$ jq '[.[] | select ((.payload.coreTransactionData.productId != null))]' paymentConfirmedEvents--FromDeals.json
How to handle non standard key names that might conflict with built-ins
Imagine the data had keys "schema:name" (i.e. with colons in them)
- This fails
json | jq '.schema:name'
However, wrapping in square brackets and strings works:
json | jq '.["schema:name"]'
How to handle when a key sometimes has an array and sometimes a single object
Use max to get last item in array. If it fails, ignore error with ?. Use the alternative operator // to fall back to . which (I THINK) is a no-op here.
cat ../transparenzportal-hamburg/freeformatter-out.json| jq '.events.Event[] | .eventDates.EventDate | (max? // .) .date'
Как удалить двойные кавычки в выводе
jq -r '.name'