Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic jq usage. How to get nested value

Tags:

json

key

jq

This must be incredibly simple but the man page makes no sense to me. curl example.com/json gives me

{
 "stats": {
  "storage_server.disk_total": XXXXXXXXXX
 },
 "counters": {}
}

and I want to extract the value XXXXXXXXXX of the disk_total. What is the syntax to do this?

like image 954
nullUser Avatar asked Jan 18 '17 05:01

nullUser


1 Answers

To get deeply nested values by their key:

$ jq '.. |."storage_server.disk_total"? | select(. != null)'

.. is a shortcut for the zero-argument recurse -- an analog of the XPath // operator.

like image 152
jfs Avatar answered Nov 15 '22 21:11

jfs