Reputation: 83
I need to transform this input into an unnested object using jq
. In Python, I could transform it properly as follows.
output = {(k1 + '-' + k2): v2 for k1, v1 in input.iteritems() for k2, v2 in v1.iteritems()}
But I could not figure out how to do this in jq
. The documentation for operating on nested objects is quite poor.
{
"group1-permission": {
"address": "test1",
"others": "test2",
"packet-capture": "test3",
"policy": "test4",
"schedule": "test5",
"service": "test6"
},
"group2-permission": {
"config": "none",
"data-access": "none",
"report-access": "none",
"threat-weight": "none"
},
"group3-permission": {
"antivirus": "none",
"application-control": "none",
"casi": "none",
"data-loss-prevention": "none",
"dnsfilter": "none",
"icap": "none",
"ips": "none",
"spamfilter": "none",
"voip": "none",
"waf": "none",
"webfilter": "none"
}
}
The output should look like this:
{
"group1-permission-address": "test1",
"group1-permission-others": "test2",
"group1-permission-packet-capture": "test3",
"group1-permission-policy": "test4",
"group1-permission-schedule": "test5",
"group1-permission-service": "test6",
"group2-permission-config": "none",
"group2-permission-data-access": "none",
"group2-permission-report-access": "none",
"group2-permission-threat-weight": "none",
"group3-permission-antivirus": "none",
"group3-permission-application-control": "none",
"group3-permission-casi": "none",
"group3-permission-data-loss-prevention": "none",
"group3-permission-dnsfilter": "none",
"group3-permission-icap": "none",
"group3-permission-ips": "none",
"group3-permission-spamfilter": "none",
"group3-permission-voip": "none",
"group3-permission-waf": "none",
"group3-permission-webfilter": "none"
}
Upvotes: 4
Views: 1478
Reputation: 116670
The documentation, in my opinion, is excellent, but it helps to have some familiarity with map
and some comfort level with pipes and filters. Anyway, here's one map-oriented solution:
to_entries
| map( .key as $key
| .value
| to_entries
| map ( { ($key + "-" + .key): .value } ) | add ) | add
Here's another, that works for arbitrarily-deeply nested JSON objects and arrays:
[paths(scalars) as $path | { ($path|join("-")): getpath($path) }] | add
There is of course a caveat here: if paths collide as a result of using "-" as the join character, some data may be lost.
Upvotes: 6