Mule DWL - 展平 json 层级元素

Mule DWL - Flatten json hierarchical elements

我有 json 有效载荷如下:

Attributes:{
    Contact:{
        Address:{
            PersonName:"John",
            Line1:"sdsds",
            Line2:"sdsdsd",
            SubAddress:{
                PersonName:"John",
                Line1:"ggghg",
                Line2:"xzxzxzx"
            }
        },
        Phone:{
            Home:"2323232323",
            Office:"4545454545"
        }
    },
    Id:"2343434",
    order:"3343434"
}

我想将层次结构展平到输出以下

Attributes:{
    Contact.Address.PersonName:"John",
    Contact.Address.Line1:"sdsds",
    Contact.Address.Line2:"sdsdsd",
    Contact.Address.SubAddress.PersonName:"John",
    Contact.Address.SubAddress.Line1:"ggghg",
    Contact.Address.SubAddress.Line2:"xzxzxzx",
    Contact.Phone.Home:"2323232323",
    Contact.Phone.Office:"4545454545",
    Id:"2343434",
    order:"3343434"
}

A​​ttributes 元素可以包含任意数量的复杂元素,如 "Address" 和 "Contact"。我们不会在编码时知道复杂元素的键值。 DWL 应该能够产生单电平输出。想要在 Mule 3 中使用 dw1 进行扁平化的通用解决方案。请帮忙。

以下脚本递归地遍历有效负载并构造单个键值 对对象

%dw 1.0
%output application/json
%var payload = {"Attributes":{"Contact":{"Address":{"SubAddress":[{"PersonName1":"John","Line1":"ggghg","Line2":"xzxzxzx"},{"PersonName":"Jar","Line1":"ggghg","Line2":"xzxzxzx"}]},"Phone":{"Home":"2323232323","Office":"4545454545"}},"Id":"2343434","order":"3343434"}}
%function constructKeys(oldKeys, newKey) oldKeys ++ '.' ++ newKey
%function writeData(json ,output, keys)  json match {
            case is :null -> null,
            case is :array -> {(json map ((item, index) -> writeData(item, output, keys ++ index)))},
            case is :object -> {((json pluck $$) map writeData(json[$],output,constructKeys(keys,$)))},
            default -> output ++ {(keys[1 to -1]): json }
}
---
Attributes: writeData(payload.Attributes,{}, '')