jq: update page (#7945)

* Fix manual url

* Replace all old examples with new ones:
- executing expression/script
- accessing keys/array items
- mutating objects

* Remove backslash, but:
- syntax highlighting is broken

* Add russian jq translation

* Fix placeholders in translation

* Specify multiple array items/keys in examples

* Describe --arg option

* Join two examples:
- first
- fourth

* Fix generic command placeholders:
- use `cat path/to/file.json`
feature/refresh-chsh
Emily Grace Seville 2022-04-13 11:45:18 +10:00 committed by GitHub
parent 3f7ce811f7
commit f0dacdbd20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 19 deletions

32
pages.ru/common/jq.md Normal file
View File

@ -0,0 +1,32 @@
# jq
> Процессор JSON командной строки, использующий доменный язык.
> Больше ифнормации: <https://stedolan.github.io/jq/manual/>.
- Выполнить указанное выражение (вывести цветной и отформатированный json):
`{{cat путь/к/файлу.json}} | jq '{{.}}'`
- Выполнить указанный скрипт:
`{{cat путь/к/файлу.json}} | jq --from-file {{путь/к/скрипту.jq}}'`
- Передать указанные агрументы:
`{{cat путь/к/файлу.json}} | jq {{--arg "имя1" "значение1" --arg "имя2" "значение2" ...}} '{{. + $ARGS.named}}'`
- Вывести указанные ключи:
`{{cat путь/к/файлу.json}} | jq '{{.ключ1, .ключ2, ...}}'`
- Вывести указанные элементы массива:
`{{cat путь/к/файлу.json}} | jq '{{.[индекс1], .[индекс2], ...}}'`
- Вывести все элементы массива/ключи объекта:
`{{cat путь/к/файлу.json}} | jq '.[]'`
- Добавить/удалить указанные ключи:
`{{cat путь/к/файлу.json}} | jq '{{.}} {{+|-}} {{{"ключ1": "значение1", "ключ2": "значение2", ...}}}'`

View File

@ -1,36 +1,32 @@
# jq
> A command-line JSON processor that uses a domain-specific language.
> More information: <https://stedolan.github.io/jq>.
> More information: <https://stedolan.github.io/jq/manual/>.
- Output a JSON file, in pretty-print format:
- Execute the specified expression (print a colored and formatted json):
`jq . {{file.json}}`
`{{cat path/to/file.json}} | jq '{{.}}'`
- Output all elements from arrays (or all the values from objects) in a JSON file:
- Execute the specified script:
`jq '.[]' {{file.json}}`
`{{cat path/to/file.json}} | jq --from-file {{path/to/script.jq}}'`
- Output elements from arrays which match provided filter:
- Pass the specifed arguments:
`jq '.[] | select(.{{key_name}} == {{value}})'`
`{{cat path/to/file.json}} | jq {{--arg "name1" "value1" --arg "name2" "value2" ...}} '{{. + $ARGS.named}}'`
- Read JSON objects from a file into an array, and output it (inverse of `jq .[]`):
- Print the specifed keys:
`jq --slurp . {{file.json}}`
`{{cat path/to/file.json}} | jq '{{.key1, .key2, ...}}'`
- Output the first element in a JSON file:
- Print the specifed array items:
`jq '.[0]' {{file.json}}`
`{{cat path/to/file.json}} | jq '{{.[index1], .[index2], ...}}'`
- Output the value of a given key of each element in a JSON text from stdin:
- Print all array items/object keys:
`cat {{file.json}} | jq 'map(.{{key_name}})'`
`{{cat path/to/file.json}} | jq '.[]'`
- Output the value of multiple keys as a new JSON object (assuming the input JSON has the keys `key_name` and `other_key_name`):
- Add/remove the specified keys:
`cat {{file.json}} | jq '{{{my_new_key}}: .{{key_name}}, {{my_other_key}}: .{{other_key_name}}}'`
- Output the value of a given key to a string (and disable JSON output):
`cat {{file.json}} | jq --raw-output '"some text: \(.{{key_name}})"'`
`{{cat path/to/file.json}} | jq '{{.}} {{+|-}} {{{"key1": "value1", "key2": "value2", ...}}}'`