37

My json looks like this :

{
  "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 from time to time. So for example 20160522201409-jobsv1-1 may be something else tomorrow. Also I may more than one such entry in the json payload.

I want to echo $KEYS and I am trying to do it using jq.

Things I have tried : | jq .KEYS is the command i use frequently.

Is there a jq command to display all the primary keys in the json?

I only care about the hostname field. And I would like to extract that out. I know how to do it using grep but it is NOT a clean approach.

sudhishkr
  • 3,318
  • 5
  • 33
  • 55
  • Is there a reason you need to know that key, rather than just directly querying through to the things underneath it? – Charles Duffy Jun 01 '16 at 20:06
  • 1
    (BTW, an all-caps shell variable name such as `$KEYS` is bad form; the POSIX spec for environment variables specifies that it's the namespace of *lower-case* names reserved for application use; since shell variables and environment variables share a namespace, following this convention avoids collisions. See http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap08.html) – Charles Duffy Jun 01 '16 at 20:07
  • I am interested in the hostname, and i dont care about the keys. And I can have multiple entries in the json. – sudhishkr Jun 01 '16 at 20:07
  • Ahh! Going straight to the hostnames is easy. :) – Charles Duffy Jun 01 '16 at 20:08

1 Answers1

53

You can simply use: keys:

% jq 'keys' my.json
[
  "20160522201409-jobsv1-1"
]

And to get the first:

% jq -r 'keys[0]' my.json
20160522201409-jobsv1-1

-r is for raw output:

--raw-output / -r: With this option, if the filter’s result is a string then it will be written directly to standard output rather than being formatted as a JSON string with quotes. This can be useful for making jq filters talk to non-JSON-based systems.

Source

If you want a known value below an unknown property, eg xxx.hostName:

% jq -r '.[].hostName' my.json
20160522201409-jobsv1-1
Andreas Louv
  • 46,145
  • 13
  • 104
  • 123
  • You might also mention how to get, say, an `ipAddress` without knowing the top-layer key. – Charles Duffy Jun 01 '16 at 20:07
  • You have the right idea, but I think the OP is actually looking for `jq -r 'keys[]' my.json`. – ruakh Jun 01 '16 at 20:08
  • ...actually, the OP has clarified in the comments on the question: "I am interested in the hostname, and [I] don't care about the keys"; thus it would be `.[].hostName` of interest. – Charles Duffy Jun 01 '16 at 20:09