Reputation: 25
i have json:
{
"error": 0,
"descErr": "Ok",
"seqNumber": 0,
"data": {
"events": [],
"devices": {
"aaa1234a": {
"deviceType": "aaa",
"ip": "192.168.1.4",
"otherInformation": "blabla4"
},
"aaa1235a": {
"deviceType": "aaa",
"ip": "192.168.1.5",
"otherInformation": "blabla5"
},
"aaa1236a": {
"deviceType": "aaa",
"ip": "192.168.1.6",
"otherInformation": "blabla6"
}
}
}
}
and want to get only device name (into json look like aaa1234a), ip
and otherInformation
with jq.
i need a result:
"aaa1234a": {
"ip": "192.168.1.4",
"otherInformation": "blabla4"
},
"aaa1235a": {
"ip": "192.168.1.5",
"otherInformation": "blabla5"
},
"aaa1236a": {
"ip": "192.168.1.6",
"otherInformation": "blabla6"
}
How is filtering this json with jq, if device name i do not know, but look like "aaa1234a", where aaa - type device and last "a" - model device - 1234 - dynamic name
Upvotes: 1
Views: 455
Reputation: 116870
To select the keys matching the regex pattern "^aaa.*a$", you could write:
.devices | with_entries(select(.key|test("^aaa.*a$")))
Similarly, if you wanted to retain the original structure:
.data.devices |= with_entries(select(.key|test("^aaa.*a$")))
Upvotes: 0
Reputation: 48
https://jqplay.org/s/n1kf-19yFT
With the input
{
"error": 0,
"descErr": "Ok",
"seqNumber": 0,
"data": {
"events": [],
"devices": {
"aaa1234a": {
"deviceType": "aaa",
"ip": "192.168.1.4",
"otherInformation": "blabla4"
},
"aaa1235a": {
"deviceType": "aaa",
"ip": "192.168.1.5",
"otherInformation": "blabla5"
},
"aaa1236a": {
"deviceType": "aaa",
"ip": "192.168.1.6",
"otherInformation": "blabla6"
}
}
}
}
When I execute the query
.data.devices | to_entries | map({key, value: {ip: .value.ip, otherInformation: .value.otherInformation}}) | from_entries
I've got the output
{
"aaa1234a": {
"ip": "192.168.1.4",
"otherInformation": "blabla4"
},
"aaa1235a": {
"ip": "192.168.1.5",
"otherInformation": "blabla5"
},
"aaa1236a": {
"ip": "192.168.1.6",
"otherInformation": "blabla6"
}
}
Is this what you're expecting?
Upvotes: 1