diff --git a/pages.ru/common/jq.md b/pages.ru/common/jq.md new file mode 100644 index 000000000..78aff2bb4 --- /dev/null +++ b/pages.ru/common/jq.md @@ -0,0 +1,32 @@ +# jq + +> Процессор JSON командной строки, использующий доменный язык. +> Больше ифнормации: . + +- Выполнить указанное выражение (вывести цветной и отформатированный 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", ...}}}'` diff --git a/pages/common/jq.md b/pages/common/jq.md index 59213ddb4..b23468418 100644 --- a/pages/common/jq.md +++ b/pages/common/jq.md @@ -1,36 +1,32 @@ # jq > A command-line JSON processor that uses a domain-specific language. -> More information: . +> More information: . -- 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", ...}}}'`