如何使用父项中的字段获取所有子元素的列表

Gwy*_*idD 10 jmespath

我有一个dicts列表,其中包含一个字段中的另一个列表.我想"扁平化"该列表,因此它为每个子元素提供了一个字段(或一些字段)从父项复制到其中.例:

来源数据:

[
    {
        "name": "A",
        "foo": "x",
        "bar": 1,
        "subelements": [
            {
                "baz": "xyz",
                "foobar": "abc"
            },
            {
                "baz": "zzz",
                "foobar": "def"
            }
        ]
    },
    {
        "name": "B",
        "foo": "Y",
        "bar": 4,
        "subelements": [
            {
                "baz": "yyy",
                "foobar": "aaa"
            },
            {
                "baz": "xxx",
                "foobar": "bbb"
            },
            {
                "baz": "www",
                "foobar": "bbb"
            }
        ]
    }
]
Run Code Online (Sandbox Code Playgroud)

预期结果:

[
    {
        "baz": "xyz",
        "foobar": "abc",
        "foo": "x"
    },
    {
        "baz": "zzz",
        "foobar": "def",
        "foo": "x"
    },
    {
        "baz": "yyy",
        "foobar": "aaa",
        "foo": "Y"
    },
    {
        "baz": "xxx",
        "foobar": "bbb",
        "foo": "Y"
    },
    {
        "baz": "www",
        "foobar": "bbb",
        "foo": "Y"
    }
]
Run Code Online (Sandbox Code Playgroud)

TRi*_*iNE 5

如果没有父节点引用,目前无法做到这一点。父节点访问仍列为功能请求