3

I have the following JSON output:

{
  "201110131738QP27N": {
    "parent": 17,
    "name": "CentralServer",
    "status": "Active",
    "count": 6
  },
  "201803271459ICV69": {
    "name": "subaccount1",
    "status": "Active",
    "count": 1
  },
  "2018032715008ZM2G": {
    "name": "subaccount2",
    "status": "Active",
    "count": 1
  },
  "201803281536PSKR4": {
    "name": "Este e um teste",
    "status": "Active"
  }
}

And I'm trying to get only the following data:

201110131738QP27N
201803271459ICV69
2018032715008ZM2G
201803281536PSKR4

To get the desired data, I'm trying to use the jq command, but so far I have not succeeded.

peak
  • 105,803
  • 17
  • 152
  • 177
  • 1
    Hi, Welcome to Stack Overflow. You mentioned you attempted this task using `jq`, but you did not provide your attempt. Could you please update your question to show your attempt? – kvantour Mar 29 '18 at 12:43

2 Answers2

7

To get the raw output without an array use |.[] to unwrap it

jq --raw-output 'keys | .[]'  input.json

produces

201110131738QP27N
201803271459ICV69
2018032715008ZM2G
201803281536PSKR4

or more simply written as just jq --raw-output 'keys[]'

Inian
  • 80,270
  • 14
  • 142
  • 161
2

Use the keys builtin function of jq.

Assuming your input JSON is stored in the file input.json:

$ cat input.json | jq 'keys'

produces:

[
  "201110131738QP27N",
  "201803271459ICV69",
  "2018032715008ZM2G",
  "201803281536PSKR4"
]
axiac
  • 68,258
  • 9
  • 99
  • 134
  • 1
    Note that `keys` sorts the key names; if you want the unsorted listing, use `keys_unsorted` instead. – peak Mar 29 '18 at 13:13