EDIT: After clarification in the comments, to retrieve the states of devices whose key begins with a "dimmer", use
jq '[ .devices | to_entries[] | select(.key | startswith("dimmer")) | .value = .value.state ] | from_entries' filename.json
Output:
{ "dimmer1": "off", "dimmer2": "off" }
This works as follows:
.devices selects the .devices attribute of a .devices objectto_entries blows up an object into an array of key-value pairs describing its attributes (devices), which means that the attribute "foo": "bar" becomes the object { "key": "foo", "value": "bar" } , and the exploded object expands into an array of such objects (one for each attribute)to_entries[] unpacks this array to pass it throughselect(.key | startswith("dimmer")) , which selects devices whose key begins with dimmer.value = .value.state restructures a key-value pair describing the device, so that the value is replaced only by its state attribute[ all that ] does a JSON array of all this and[ all that ] | from_entries [ all that ] | from_entries converts an array of key-value pairs back to JSON objects.
The old answer (shortened), now obsolete, but perhaps interesting:
To get the device attribute keys in an array:
jq '.devices | keys' filename.json
To get the values ββ(also in the array),
jq '[ .devices[] ]' filename.json
I was not sure which of the two you had in mind.
source share