json - How to use `jq` to obtain the keys -
my json looks :
{ "20160522201409-jobsv1-1": { "vmstatedisplayname": "ready", "servers": { "20160522201409 jobs_v1 1": { "serverstatedisplayname": "ready", "creationdate": "2016-05-22t20:14:22.000+0000", "state": "ready", "provisionstatus": "pending", "serverrole": "role", "servertype": "server", "servername": "20160522201409 jobs_v1 1", "serverid": 2902 } }, "isadminnode": true, "creationdate": "2016-05-22t20:14:23.000+0000", "totalstorage": 15360, "shapeid": "ot1", "state": "ready", "vmid": 4353, "hostname": "20160522201409-jobsv1-1", "label": "20160522201409 jobs_v1 admin_server 1", "ipaddress": "10.252.159.39", "publicipaddress": "10.252.159.39", "usagetype": "admin_server", "role": "admin_server", "componenttype": "jobs_v1" } }
my key keeps changing time time. example 20160522201409-jobsv1-1
may else tomorrow. may more 1 such entry in json payload.
i want echo $keys
, trying using jq
.
things have tried : | jq .keys
command use frequently.
is there jq command display primary keys in json?
i care hostname
field. , extract out. know how using grep
not clean approach.
you can use: keys
:
% jq 'keys' my.json [ "20160522201409-jobsv1-1" ]
and first:
% jq -r 'keys[0]' my.json 20160522201409-jobsv1-1
-r
raw output:
--raw-output / -r:
option, if filter’s result string written directly standard output rather being formatted json string quotes. can useful making jq filters talk non-json-based systems.
if want known value below unknown property, eg xxx.hostname
:
% jq -r '.[].hostname' my.json 20160522201409-jobsv1-1