JSON - mwicat/personal GitHub Wiki
Query
- JQ cheatsheet
- https://stedolan.github.io/jq/manual/
- https://github.com/pkoppstein/jq/wiki/A-Stream-oriented-Introduction-to-jq
- https://programminghistorian.org/en/lessons/json-and-jq
sudo apt install jq
Get all items under key
$ k get pod mypod -o yaml | yq -y '.. | .resources? //empty'
limits:
cpu: '1'
memory: 1Gi
requests:
cpu: '1'
memory: 1Gi
Find all objects under given key name
https://github.com/stedolan/jq/wiki/FAQ#general-questions
𝑸: What is the equivalent of XPath's // expression? How can I find the value of a given key, no matter how deeply nested the object is? How can I find the path to a slot?
Get values of all keys named "resources" and print together with root element
yq -y '. as $parent | .. | .resources? //empty | $parent.metadata, .' < skaffold_render.yaml
List keys
cat document.json | jq keys
For each key in document, extract mappings/doc/properties/tags
cat document.json | jq '.[] | .mappings.doc.properties.tags
Canonical representation (with sort keys)
python -c 'import json, sys; print(json.dumps(json.load(sys.stdin), sort_keys=True, indent=4))' < m1.json
or:
sudo cpan install JSON::PP
json_pp -json_opt pretty,canonical < $1