0

I am doing the transformation from json to xml then have to group by department. I am using mysql database to get the employee and it's department. At the last transformer I have to group by Department.

This is my json that I am getting from database after doing transformation.

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

Then, I am doing json to xml transformation and getting this below result.

<?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>

Expected result: I need to show the result group by Department wise dynamically like below-

<?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>

How can I do this transformation?

utechtzs
  • 1,013
  • 5
  • 12
Devendra
  • 219
  • 2
  • 22

1 Answers1

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
            }
        })
    }
}
utechtzs
  • 1,013
  • 5
  • 12
  • I am getting error Cannot coerce a :null to a :string – Devendra May 06 '19 at 17:56
  • I went from the json, not the second xml. I am using "Full Name" not "FullName". Maybe it's the space. – utechtzs May 06 '19 at 17:57
  • yes json to xml is working. I am using same for xml to xml with making 'Full Name' to 'FullName'. will that not work? – Devendra May 06 '19 at 18:06
  • 1
    I updated my answer. Also, the second xml in the question didn't have DepartmentName in the second employee, which was throwing off the dataweave. If it works, please mark the answer as accepted. Thanks! – utechtzs May 06 '19 at 19:00