How to get key names from JSON using jq

JsonShellUnixKeyJq

Json Problem Overview


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

Json Solutions


Solution 1 - Json

You can use:

jq 'keys' file.json

Complete example

$ cat file.json
{ "Archiver-Version" : "Plexus Archiver", "Build-Id" : "", "Build-Jdk" : "1.7.0_07", "Build-Number" : "", "Build-Tag" : "", "Built-By" : "cporter", "Created-By" : "Apache Maven", "Implementation-Title" : "northstar", "Implementation-Vendor-Id" : "com.test.testPack", "Implementation-Version" : "testBox", "Manifest-Version" : "1.0", "appname" : "testApp", "build-date" : "02-03-2014-13:41", "version" : "testBox" }

$ jq 'keys' file.json
[
  "Archiver-Version",
  "Build-Id",
  "Build-Jdk",
  "Build-Number",
  "Build-Tag",
  "Built-By",
  "Created-By",
  "Implementation-Title",
  "Implementation-Vendor-Id",
  "Implementation-Version",
  "Manifest-Version",
  "appname",
  "build-date",
  "version"
]

UPDATE: To create a BASH array using these keys:

Using BASH 4+:

mapfile -t arr < <(jq -r 'keys[]' ms.json)

On older BASH you can do:

arr=()
while IFS='' read -r line; do
   arr+=("$line")
done < <(jq 'keys[]' ms.json)

Then print it:

printf "%s\n" ${arr[@]}

"Archiver-Version"
"Build-Id"
"Build-Jdk"
"Build-Number"
"Build-Tag"
"Built-By"
"Created-By"
"Implementation-Title"
"Implementation-Vendor-Id"
"Implementation-Version"
"Manifest-Version"
"appname"
"build-date"
"version"

Solution 2 - Json

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

echo '{"data": "1", "user": { "name": 2, "phone": 3 } }' | jq '.user | keys[]'
"name"
"phone"

Solution 3 - Json

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"

Solution 4 - Json

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;

Solution 5 - Json

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"

Solution 6 - Json

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

ab
cd

Solution 7 - Json

If your input is an array of objects,

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

try with:

jq '.[] | keys[]'

Solution 8 - Json

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

Solution 9 - Json

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'

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEzhilan MahalingamView Question on Stackoverflow
Solution 1 - JsonanubhavaView Answer on Stackoverflow
Solution 2 - JsonGianfranco P.View Answer on Stackoverflow
Solution 3 - JsonChris StryczynskiView Answer on Stackoverflow
Solution 4 - JsonElliot PahlView Answer on Stackoverflow
Solution 5 - JsonnrbView Answer on Stackoverflow
Solution 6 - JsonhrushikeshView Answer on Stackoverflow
Solution 7 - JsonfreedevView Answer on Stackoverflow
Solution 8 - JsonpeakView Answer on Stackoverflow
Solution 9 - JsonRon MartinezView Answer on Stackoverflow