简体   繁体   English

如何使用jq将此嵌套对象转换为扁平对象?

[英]How do I transform this nested object to a flattened object using jq?

I need to transform this input into an unnested object using jq . 我需要使用jq将此输入转换为未嵌套的对象。 In Python, I could transform it properly as follows. 在Python中,我可以按以下方式正确对其进行转换。

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 . 但是我不知道如何在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"
}

The documentation, in my opinion, is excellent, but it helps to have some familiarity with map and some comfort level with pipes and filters. 我认为该文档非常出色,但有助于您熟悉map以及对管道和过滤器的舒适度。 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: 这是另一个适用于任意深度嵌套的JSON对象和数组的方法:

[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. 当然这里有一个警告:如果由于使用“-”作为连接字符而导致路径冲突,则可能会丢失一些数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM