0

我正在做从 json 到 xml 的转换,然后必须按部门分组。我正在使用 mysql 数据库来获取员工及其部门。在最后一个变压器上,我必须按部门分组。

这是我在进行转换后从数据库中获取的 json。

[
    {
        "id": 1,
        "Full Name": "Devendra",
        "Department": {
            "DepartmentName": "IT"
        }
    },
    {
        "id": 2,
        "Full Name": "Rohit",
        "Department": {
            "DepartmentName": "IT"
        }
    }
]

然后,我正在做 json 到 xml 的转换并得到下面的结果。

<?xml version="1.0" encoding="windows-1252"?>
<employees>
    <employee>
        <id>1</id>
        <FullName>Devendra</FullName>
        <Department>
            <DepartmentName>IT</DepartmentName>
        </Department>
    </employee>
    <employee>
        <id>2</id>
        <FullName>Rohit</FullName>
        <Department>
            <DepartmentName>IT</DepartmentName>
        </Department>
    </employee>
</employees>

预期结果:我需要按部门动态显示结果组,如下所示-

<?xml version="1.0" encoding="windows-1252"?>
<Departments>
    <Department>
        <DepartmentName>IT</DepartmentName>
        <employee>
            <id>1</id>
            <FullName>Devendra</FullName>
        </employee>
        <employee>
            <id>2</id>
            <FullName>Rohit</FullName>
        </employee>
    </Department>
</Departments>

我该如何进行这种转变?

4

1 回答 1

1

json2xml:

%dw 1.0
%output application/xml
---
Departments: payload groupBy $.Department.DepartmentName mapObject {
    Department: 
        {DepartmentName: $$} ++
        {($ map {
            employee: {
                id: $.id,
                FullName: $.'Full Name'
            }
        })
    }
}

xml2xml:

%dw 1.0
%output application/xml
---
Departments: payload.employees groupBy $.Department.DepartmentName mapObject {
    Department: 
        {DepartmentName: $$} ++
        {($ map {
            employee: {
                id: $.id,
                FullName: $.FullName
            }
        })
    }
}
于 2019-05-06T17:46:15.743 回答