39

I've got JSON that looks like this

{
  "keyword1": {
    "identifier1": 16
  },
  "keyword2": {
    "identifier2": 16
  }
}

and I need to loop through the keywords to get the identifiers (not sure if I'm using the right terminology here). Seems pretty simple, but because the keywords are all named different, I don't know how to handle that.

2
  • 1
    Please specify what you want the output to look like.
    – Mark Reed
    Commented Jan 30, 2016 at 4:09
  • Just like the answer from peak Commented Feb 1, 2016 at 2:25

4 Answers 4

56

The original tag for this question was jq so here is a jq solution:

.[] | keys[]

For example, with the input as shown in the question:

$ jq '.[] | keys[]' input.json

"identifier1"
"identifier2"

To retrieve the key names in the order they appear in the JSON object, use keys_unsorted.

1
  • Neat, I didn't know that you could turn an object into an array with [].
    – Mark Reed
    Commented Feb 1, 2016 at 14:21
36

I'd think something along these lines would work well:

jq '. | to_entries | .[].key'

see https://jqlang.github.io/jq/manual/#to_entries-from_entries-with_entries

or if you wanted to get the values from a variable:

JSON_DATA={main:{k1:v1,k2:v2}}
result=$(jq -n "$JSON_DATA" | jq '.main | to_entries | .[].value' --raw-output)
echo $result

##outputs: v1 v2
1
  • 1
    Usage of to_entries was very helpful! Commented Jul 21, 2020 at 9:04
5

I came here hoping to sort out a bunch of keys from my JSON, I found two features handy. There are three functions "to_entries", "from_entries", and "with_entries". You can filter the values by key or value, like so:

JSON_DATA='
{
  "fields": {
    "first": null,
    "second": "two",
    "third": "three"
  }
}
'

echo "$JSON_DATA" | jq '{fields: .fields | with_entries(select(.value != null and .key != "third")) }'

Output:

{
  "fields": {
    "second": "two"
  }
}
1

simpler solution - just treat internal hash as a new hash and add one more filter. The query that helped me:

$ docker network inspect bridge|jq '.[].Containers'
{
  "35c9e1273c43db01c45b5f43f6999d04c18beff3996ea09fb8b87a8b635c38ff": {
    "Name": "nginx",
    "EndpointID": "a6e788d6f90eb14df2321a2eb02517f0862c1fe7fe50c02f2b8c103c0c79cb6b",
    "MacAddress": "02:42:ac:11:00:02",
    "IPv4Address": "172.17.0.2/16",
    "IPv6Address": ""
  },
  "b46c157cec243969f9227251dfd6fa65b7a904e145df80a63f79d4dc8b281355": {
    "Name": "sweet_gates",
    "EndpointID": "a600d9c1ee35b9f7db31249ae8f589c202e0b260e10a394757a88bfd66b5b42f",
    "MacAddress": "02:42:ac:11:00:03",
    "IPv4Address": "172.17.0.3/16",
    "IPv6Address": ""
  }
}

As I needed only couple of fields, add to above .json one more query:

$ docker network inspect bridge|jq -jr '.[].Containers[]|.IPv4Address, "\t", .Name, "\n"'
172.17.0.2/16   nginx
172.17.0.3/16   sweet_gates

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.