How to get key names from JSON using jq
Asked Answered
K

9

270

curl http://testhost.test.com:8080/application/app/version | jq '.version' | jq '.[]'

The above command outputs only the values as below:

"[email protected]"

"2323"

"test"

"02-03-2014-13:41"

"application"

How can I get the key names instead like the below:

email

versionID

context

date

versionName
Klug answered 16/4, 2014 at 19:21 Comment(0)
F
394

To get the keys in the order they appear in the original JSON use:

jq 'keys_unsorted' file.json

If you want the keys sorted alphanumerically, you can use:

jq 'keys' file.json

Complete example

$ cat file.json
{ "Created-By" : "Apache Maven", "Build-Number" : "", "Archiver-Version" : "Plexus Archiver", "Build-Id" : "",  "Build-Tag" : "", "Built-By" : "cporter"}

$ jq 'keys_unsorted' file.json                                         
[
  "Created-By",
  "Build-Number",
  "Archiver-Version",
  "Build-Id",
  "Build-Tag",
  "Built-By"
]

$ jq 'keys' file.json
[
  "Archiver-Version",
  "Build-Id",
  "Build-Number",
  "Build-Tag",
  "Built-By",
  "Created-By"
]
Fivepenny answered 16/4, 2014 at 19:36 Comment(3)
This is great when an API returns you something gigantic and you have to scroll up to find the name of the field at the beginning of the object.Dorena
I wonder if there's an equivalent for only a sub-json object. It only works on the top-level object.Dorena
@SridharSarnobat You can use the normal jq selector for that. Example: echo '{"root": {"z": 1, "y": 2, "x": 3}}' | jq '.root | keys'Enneahedron
E
88

To get the keys on a deeper node in a JSON:

echo '{"data": "1", "user": { "name": 2, "phone": 3 } }' | jq '.user | keys[]'
"name"
"phone"
Ernesternesta answered 21/5, 2020 at 14:38 Comment(1)
This one is a bit better than the accepted answer, as it demonstrates how to use | within a jq expression, rather than piping output through several instances of jqRealm
L
44

You need to use jq 'keys[]'. For example:

echo '{"example1" : 1, "example2" : 2, "example3" : 3}' | jq 'keys[]'

Will output a line separated list:

"example1"
"example2"
"example3"
Letendre answered 6/7, 2017 at 18:21 Comment(0)
A
21

In combination with the above answer, you want to ask jq for raw output, so your last filter should be eg.:

     cat input.json | jq -r 'keys'

From jq help:

     -r     output raw strings, not JSON texts;
Androw answered 28/6, 2017 at 6:55 Comment(0)
G
14

If your input is an array of objects,

[
  { 
    "a01" : { "name" : "A", "user" : "B" }
  },
  { 
    "a02" : { "name" : "C", "user" : "D" }
  }
]

try with:

jq '.[] | keys[]'
Gyp answered 25/4, 2021 at 6:45 Comment(0)
U
12

Oddly enough, the accepted answer doesn’t actually answer the Q exactly, so for reference, here is a solution that does:

$ jq -r 'keys_unsorted[]' file.json
Untangle answered 29/4, 2021 at 15:42 Comment(3)
This is the only one that worked for me. I spend over 6 hours just on this issue. Thank you! You can also use a JSON varaible too using: declare -a MASTER_KEYS_LIST=($(jq -r 'keys_unsorted[]' <<< "${JSON_OBJECT_VAR}"))Incontinent
Yep, this is the only one that doesn't change the order of your keys. Though the [] seems unnecessary. This works for me jq 'keys_unsorted' file.jsonOccultation
@Untangle thanks for this proper solution, I edited the accepted answer to add keys_unsorted and made the example data show the difference (OP used sorted keys which hid the difference between keys and keys_unsorted)Occultation
C
11

To print keys on one line as csv:

echo '{"b":"2","a":"1"}' | jq -r 'keys | [ .[] | tostring ] | @csv'

Output:

"a","b"

For csv completeness ... to print values on one line as csv:

echo '{"b":"2","a":"1"}' | jq -rS . | jq -r '. | [ .[] | tostring ] | @csv'

Output:

"1","2"
Coley answered 13/1, 2019 at 0:1 Comment(0)
R
8

echo '{"ab": 1, "cd": 2}' | jq -r 'keys[]' prints all keys one key per line without quotes.

ab
cd
Radcliff answered 11/1, 2019 at 19:45 Comment(0)
O
2

Here's another way of getting a Bash array with the example JSON given by @anubhava in his answer:

arr=($(jq --raw-output 'keys_unsorted | @sh' file.json))

echo ${arr[0]}    # 'Archiver-Version'
echo ${arr[1]}    # 'Build-Id'
echo ${arr[2]}    # 'Build-Jdk'
Oys answered 27/4, 2020 at 16:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.