@@ -307,7 +307,7 @@ rating (note that the first process in the pipeline outputs in json):
307307produces and outputs JSON. This gives the user an opportunity to filter
308308or modify the results:
309309
310- ** Usage Example**
310+ ** Filtering Usage Example**
311311
312312Show all authors, without showing duplicates and output in JSON format.
313313
@@ -369,6 +369,79 @@ $ ./JSONPath.sh -f test/valid/goessner.net.expanded.json \
369369 },
370370```
371371
372+ ** Filter and Merge Usage Example**
373+
374+ Different parts of JSON input, or entirely different JSON input, can be merged
375+ together with Unix 'sort' and output in json format.
376+
377+ This is a complex kubernetes example that uses JSONPath.sh and standard Unix
378+ tools to output just the command, pod mounts, and container mounts (output from
379+ different parts of the tree) for the first matched kube-proxy pod.
380+
381+ ```
382+ # Get details of all pods
383+ kubectl get -n kube-system pods -o json >/tmp/kpod
384+
385+ # Get the index of the first pod with name starting 'kube-proxy'
386+ idx=`JSONPath.sh -f /tmp/kpod '$.items[?(@.metadata.name=="kube-proxy.*")].apiVersion' \
387+ | head -n1 | grep -o ',[0-9]\+,' | tr -d ,`
388+
389+ # Get three subtrees using the index and merge them using sort
390+ # and then output in json format
391+ ( JSONPath.sh -f /tmp/kpod '$.items['$idx'].spec.volumes'; \
392+ JSONPath.sh -f /tmp/kpod '$.items['$idx']..volumeMounts'; \
393+ JSONPath.sh -f /tmp/kpod '$.items['$idx']..containers[*].command'
394+ ) | sort | JSONPath.sh -p -u
395+ ```
396+
397+ , which produces:
398+
399+ ```
400+ {
401+ "containers":
402+ [
403+ {
404+ "command":
405+ [
406+ "/usr/local/bin/kube-proxy",
407+ "--kubeconfig=/var/lib/kube-proxy/kubeconfig.conf"
408+ ],
409+ "volumeMounts":
410+ [
411+ {
412+ "mountPath":"/var/lib/kube-proxy",
413+ "name":"kube-proxy"
414+ },
415+ {
416+ "mountPath":"/var/run/secrets/kubernetes.io/serviceaccount",
417+ "name":"kube-proxy-token-m9b6j",
418+ "readOnly":true
419+ }
420+ ]
421+ }
422+ ],
423+ "volumes":
424+ [
425+ {
426+ "configMap":
427+ {
428+ "defaultMode":420,
429+ "name":"kube-proxy"
430+ },
431+ "name":"kube-proxy"
432+ },
433+ {
434+ "name":"kube-proxy-token-m9b6j",
435+ "secret":
436+ {
437+ "defaultMode":420,
438+ "secretName":"kube-proxy-token-m9b6j"
439+ }
440+ }
441+ ]
442+ }
443+ ```
444+
372445## Cool Links
373446
374447* [ dominictarr/JSON.sh] ( https://github.com/dominictarr/JSON.sh ) The original, the best, JSON.sh.
0 commit comments