Contenu connexe Similaire à CSV and JSON Transformation in WSO2 Micro Integrator 4.0 - WSO2 APIM Community Call (9/29/2021) (20) CSV and JSON Transformation in WSO2 Micro Integrator 4.0 - WSO2 APIM Community Call (9/29/2021)1. WSO2 API Manager Community Call
CSV and JSON transformation in MI 4.0.0
September 29th, 2021
4. ● Overview
● CSV Connector
● FreeMarker templates in Payload Factory mediator
● Demonstrations
What we will be covering today
4
6. Existing Methods to Tansformation
6
● Data Mapper
● PayloadFactory
● Script Mediator
● XSLT Mediator
● Class Mediator
9. Supporting Transformations in CSV Module
9
● CSV to CSV transformation
● CSV to JSON transformation
● CSV to XML transformation
● JSON to CSV transformation
● XML to CSV transformation
15. JSON to XML
15
{
"first_name": "John",
"last_name": "Deo",
"age": 35,
"location": {
"state": {
"
code": "NY",
"
name": "New York"
},
"city": "Manhattan"
}
}
<user>
<Name>John Deo</Name>
<Age>35</Age>
<Address>Manhattan, NY</
Address>
</user>
<user>
<Name>${payload.first_name} ${payload.last_name}</
Name>
<Age>${payload.age}</
Age>
<Address>${payload.location.city}, ${payload.location.state.code}</
Address>
</user>
Input Output
Template
16. XML to JSON
16
<user>
<first_name >John</first_name >
<last_name>Deo</last_name>
<age>35</age>
<location>
<state code="NY">New York</ state>
<city>Manhattan</ city>
</location>
</user>
{
"Name": "John Doe" ,
"Age": 35,
"Address": "Manhattan, NY"
}
{
"Name": "${payload.user.first_name} ${payload.user.last_name}" ,
"Age": ${payload.user.age} ,
"Address": "${payload.user.location.city},${payload.user.location.state.@code}"
}
Input Output
Template
17. XML to CSV
17
<people>
<person>
<id>1</id>
<first_name>Veronika</first_name>
<last_name>Lacroux</last_name>
</person>
<person>
<id>2</id>
<first_name>Trescha</first_name>
<last_name>Campaigne</
last_name>
</person>
</people>
ID,First Name, Last Name
1,Veronika,Lacroux
2,Trescha,Campaigne
ID,First Name, Last Name
<#list payload.people.person as person>
${person.id},${person.first_name},${person.last_name}
</#list>
Input Output
Template
18. Bit more complex transformation
18
{
"custID": "cd5322",
"custName": "wso2",
"tier": "Gold",
"items": {
"item": [
{
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": {
"batter": [
{
"id": "1001",
"type": "Regular"
},
{
"id": "1002",
"type": "Chocolate"
}
]
}
},
{
"id": "0002",
"type": "donut",
"name": "Raised",
"ppu": 0.55,
"batters": {
"batter": [
{
"id": "1001",
"type": "Regular"
}
]
}
}
]
}
}
<cd5322 >
<cake>
<custName tier="Gold"> WSO2</custName >
<id>0001</id>
<type>donut </type>
<ppu>0.55</ppu>
<batter >
<id>1001</id>
<type>Regular </type>
</batter >
<batter >
<id>1002</id>
<type>Chocolate </type>
</batter >
</cake>
<cake>
<custName tier="Gold"> WSO2</custName >
<id>0002</id>
<type>donut </type>
<ppu>0.55</ppu>
<batter >
<id>1001</id>
<type>Regular </type>
</batter >
</cake>
</cd5322 >
Input Output
19. Bit more complex transformation - FreeMarker Template
19
<${payload.custID}>
<#list payload.items.item as item>
<cake>
<custName
tier="${payload.tier}">${payload.custName?upper_case}</custName>
<id>${item.id}</id>
<type>${item.type}</type>
<ppu>${item.ppu}</ppu>
<#list item.batters.batter as batter>
<batter>
<id>${batter.id}</id>
<type>${batter.type}</type>
</batter>
</#list>
</cake>
</#list>
</${payload.custID}>
21. ● CSV Module
● Freemarker templates in PayloadFactory mediator
● Apache FreeMarker
Useful Links
21