202

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

The above command outputs only the values as below:

"madireddy@test.com"

"2323"

"test"

"02-03-2014-13:41"

"application"

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

email

versionID

context

date

versionName
peak
  • 88,177
  • 15
  • 123
  • 150
Ezhilan Mahalingam
  • 2,338
  • 3
  • 14
  • 15
  • like this . Anubhava..version : { "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", } – Ezhilan Mahalingam Apr 16 '14 at 19:28
  • Can you please add the example data that is coming out of API and is input to `jq`? – Carl G Feb 05 '19 at 14:01

9 Answers9

284

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"
SomeGuyOnAComputer
  • 4,177
  • 5
  • 33
  • 58
anubhava
  • 713,503
  • 59
  • 514
  • 593
  • 1
    Thank you it worked! I am assigning these values into array in Shell script like array=($keyContents) but its not assigning properly, Is there any way to assign values to arrays mentioning delimiters? – Ezhilan Mahalingam Apr 16 '14 at 19:56
  • Whenever the key/value has space in it, array is treating it as new value, I replaced spaces with _ which resolved the issue. Thank you very much! but now one more problem. : ( I am getting different orders when i get keys alone and when i get values alone : ( – Ezhilan Mahalingam Apr 16 '14 at 20:13
  • I am planning to get the keys and values separately in two arrays to display the output in table like format. Its my requirement. can you please help me AnuBhava. – Ezhilan Mahalingam Apr 16 '14 at 20:14
  • That requirement should go in separate question. If you write your question clearly with sample data I will try to come up with an answer. – anubhava Apr 16 '14 at 20:22
  • 2
    Use `mapfile -t arr < – Niklas Holm Apr 12 '18 at 16:18
  • Good point @NiklasHolm, though that is only supported in BASH 4+. I will edit the question with this – anubhava Apr 12 '18 at 17:42
  • 1
    @anubhava, if mapfile is unavailable a good old `arr=(); while IFS='' read -r line; do arr+=("$line"); done < – Niklas Holm Apr 13 '18 at 03:14
  • 2
    Might want to mention `keys_unsorted` to get the keys in document order. – Jeff Mercado Jan 13 '19 at 02:44
  • Is there's an easy way to get the keys for any nested depth? where `.` is replaced with '__' or something like that? – AKS Jan 18 '21 at 19:22
44

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

echo '{"data": "1", "user": { "name": 2, "phone": 3 } }' | jq '.user | keys[]'
"name"
"phone"
Gianfranco P.
  • 8,605
  • 5
  • 46
  • 64
  • 2
    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 jq – tschaible Oct 11 '21 at 13:07
39

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"
Chris Stryczynski
  • 25,295
  • 38
  • 135
  • 245
15

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;
Elliot Pahl
  • 151
  • 1
  • 3
  • This is a tricky `bash` script gotcha--if you don't specify `-r` then you get a quoted string, which looks fine on-screen, but you usually don't want the quotes embedded in the string. – MarkHu Apr 26 '18 at 02:49
  • 5
    *Useless use of cat* – 0andriy Jul 31 '18 at 01:26
9

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"
nrb
  • 182
  • 1
  • 4
6

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

ab
cd
hrushikesh
  • 167
  • 1
  • 8
6

If your input is an array of objects,

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

try with:

jq '.[] | keys[]'
freedev
  • 21,315
  • 4
  • 98
  • 111
5

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
peak
  • 88,177
  • 15
  • 123
  • 150
  • 1
    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}"))` – Jeremy May 22 '22 at 14:53
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'
Ron Martinez
  • 195
  • 3
  • 7