{{tag>Brouillon Python CA}} = Notes JSON et YAML | **jc** | JSON Convert JSONifies the output of many CLI tools and file-types | | **jq** | Command-line JSON processor | | **json_pp** | JSON Pretty Printer | | **JMESPath** | JMESPath is a query language for JSON (utilisé par ''json_query'' d'Ansible | | **jp** | The ''jp'' command is a command line interface to JMESPath | Voir : * JSONPath et go_template * JSON RFC 6902 patch Voir Ansible : * https://blog.stephane-robert.info/post/ansible-filtres-advanced-2/ * https://docs.ansible.com/ansible/latest/collections/community/general/docsite/filter_guide_selecting_json_data.html Voir aussi : * [[https://mgree.github.io/ffs/|Monter un fichier JSON comme en système de fichier avec ffs]] sudo apt-get install jc dig www.google.com | jc --dig -p [ { "id": 17598, "opcode": "QUERY", "status": "NOERROR", "flags": [ "qr", "rd", "ra" ], "query_num": 1, "answer_num": 1, "authority_num": 0, "additional_num": 1, "opt_pseudosection": { "edns": { "version": 0, "flags": [], "udp": 4096 } }, "question": { "name": "www.google.com.", "class": "IN", "type": "A" }, "answer": [ { "name": "www.google.com.", "class": "IN", "type": "A", "ttl": 38, "data": "142.250.178.132" } ], "query_time": 8, "server": "208.67.222.123#53(208.67.222.123) (UDP)", "when": "Sun Oct 16 21:54:04 CEST 2022", "rcvd": 59, "when_epoch": 1665950044, "when_epoch_utc": null } ] cat fic.json |python3 -m json.tool |native2ascii -encoding UTF-8 -reverse Extension Firefox http://jsonview.com/ sudo apt-get install yajl-tools yajl-tools * json_reformat * json_verify mongoimport --host localhost --db database --collection collection Convert JSON to YAML yq -P sample.json Ou encore python -c 'import sys, yaml, json; yaml.safe_dump(json.load(sys.stdin), sys.stdout, default_flow_style=False)' < /opt/stack/keystone/etc/policy.v3cloudsample.json > /etc/keystone/policy.yaml pip install jsbeautifier js-beautify file.js Dico vers JSON ? cat plop.t |tr \' \" |sed -e 's/None/""/g' |jq . VIM - Formater le JSON :%!python -m json.tool == Requête (Query) Avec curl curl -k https://aap-controller.local/api/v2/users/27/roles/ -X POST -u user:password --data-raw '{"id":534}' Avec **jq** voir : https://hyperpolyglot.org/json Top level sections cat single-instance.json |jq 'keys[]' Exemple wildcard $ curl -s -u "${AAP_USER}:${AAP_PASS}" "https://aap.acme.local/api/v2/job_templates/" | jq '.results[0].id' 1680 $ curl -s -u "${AAP_USER}:${AAP_PASS}" "https://aap.acme.local/api/v2/job_templates/" | jq '.results[].id' | tail -3 1572 1569 2343 jq retours sans guillemet (quote) : jq -r docker inspect portainer/portainer-ce |jq '.[0].RepoTags' ansible -i testsrv, -m setup all |sed -e 's/^.*=>//' |jq -r '.ansible_facts.ansible_mounts[] |.mount' === Filtres Conversion string integer avec ''tonumber'' Exemple jq '.blockdevices[].size | tonumber' == Autres === Ansible - Utilisation de boolean en extra vars via JSON ansible-playbook playbook.yml -i inventory.ini -e '{ "remove_vhost": false }' == YAML | **yq** | portable command-line YAML processor | | **[[https://gitlab.com/t0pd4wn/yb|yb]]** | YAML Parser in bash | Voir : * https://yamllint.readthedocs.io/en/stable/rules.html#module-yamllint.rules.colons * https://stackoverflow.com/questions/3790454/how-do-i-break-a-string-in-yaml-over-multiple-lines/21699210#21699210 Contrôle syntaxe : * [[https://morioh.com/p/4d20ac5ddcc0|Yamale]] Ce que **jq** est à json, **yq** l'est pour le yaml. **yq** permet également de convertir des fichiers JSON en YAML cat .kube/config |egrep -v "\-data" |yq . # pip install --user yq sudo apt-get install yq openstack image show IMAGE1 -c properties -f yaml |yq '.properties.direct_url' Filtrer sur les clefs (keys) cat meta/runtime.yml | yq '.action_groups | keys' # Ou cat meta/runtime.yml | yq -r '.action_groups | keys[]' Mofifier un fichier YAML avec yq : * https://www.baeldung.com/linux/yq-utility-processing-yaml * https://dev.to/vikcodes/yq-a-command-line-tool-that-will-help-you-handle-your-yaml-resources-better-8j9 En Python et autres : * https://stackoverflow.com/questions/63581308/edit-yaml-file-with-bash Avec Ansible : https://github.com/kwoodson/ansible-role-yedit Exemple non-specific tag configuration: !include config.d/*.yaml Reférences vars: webapp: version: &my_version 1.0 custom_name: - "ToDo_App" - *my_version === Exemple Python import yaml with open("filter.yml", "r") as yamlfile: #filters = yaml.load(yamlfile, Loader=yaml.FullLoader) filters = yaml.safe_load(yamlfile) == Échapper certains caractères === Passer un fichier YAML à une API REST qui fonctionne en JSON cat inv.yaml |sed -e 's/$/\\n/' -e 's/"/\\"/g' |tr -d '\n' ==== Exemple YAML vers JSON ''inv.yaml'' --- server1: hosts: server1.inf.acme.lan: vars: SET_LINGERING_LIST: - SET_LINGERING_USER: testplop SET_LINGERING_ENABLED: "false" - SET_LINGERING_USER: testplop SET_LINGERING_ENABLED: "true" type_host: hosts: server1.inf.acme.lan: {} vars: type: host #! /bin/bash set -euo pipefail YAML="$(cat inv.yaml |sed -e 's/$/\\n/' -e 's/"/\\"/g' |tr -d '\n')" curl -v -k -u user1:'P@ssw0rd' -H 'Content-Type: application/json' -X POST https://awx.acme.fr/api/v2/job_templates/81/launch/ -d '{ "extra_vars": { "foo1": "bar1", "foo2": "bar2", "inventory_content": "'"${YAML}"'" } }' Ou avec un heredoc YAML="$(cat <<'EOF' | sed -e 's/$/\\n/' -e 's/"/\\"/g' |tr -d '\n' --- server1: hosts: server1.inf.acme.lan: vars: SET_LINGERING_LIST: - SET_LINGERING_USER: testplop SET_LINGERING_ENABLED: "false" - SET_LINGERING_USER: testplop SET_LINGERING_ENABLED: "true" type_host: hosts: server1.inf.acme.lan: {} vars: type: host EOF )" ==== En convertissant le YAML en JSON En passant un JSON dans un JSON en string #JS="$(ansible-inventory -i inv.yaml --list)" JS="$(cat inv.js)" JS="$(echo $JS |sed 's/"/\\"/g')" URL='' curl -v -k -u user1:'P@ssw0rd' -H 'Content-Type: application/json' -X POST https://awx.acme.fr/api/v2/job_templates/81/launch/ -d '{ "extra_vars": { "foo1": "bar1", "foo2": "bar2", "inventory_content": "'"${JS}"'" } }' === Exemple de yaml CRON_HOST_TARGETS: all: - CRON_FILENAME: crontab_app1 CRON_USER: app1 CRONS: - DESCRIPTION: "topmem.sh exécuté toutes les heures" SCHED: "0 * * * *" CMDLINE: "~/scripts/topmem.sh >> ~app1/logs/topmem.out 2>&1" sc01: - CRON_FILENAME: crontab_app1_sc01 CRON_USER: app1 CRONS: - DESCRIPTION: "archivage de certains fichiers applicatifs" SCHED: "06 03 * * *" CMDLINE: ". ~/scripts/archive_plopsc.sh -l${LINE} >> ~app1/logs/archive_sc_${LINE}.out 2>&1" - DESCRIPTION: Performances SCHED: "05 03 * * *" CMDLINE: ". ~/scripts/appexec.sh appenv.sh ${LINE} > /dev/null; ~/scripts/stat_report_run.sh ${LINE} > /dev/null" == Pb === Compile Error - Échapper certains caractères ''plop.yml'' rec:plop: stage: plop variables: ENV: "RECETTE" prd:plop: stage: plop variables: ENV: "PRODUCTION" $ cat plop.yml |yq '.rec:plop' jq: error: syntax error, unexpected ':', expecting $end (Unix shell quoting issues?) at , line 1: .rec:plop jq: 1 compile error ==== Solution cat plop.yml |yq '.["rec:plop"]'