Monday, April 11, 2016

MuleSoft Dataweave Tutorial: Part I - JSON To XML Example

Use Case One

Input

{
    "medication": {
        "name": "Natrecor"
    },
    "genericequivalents": [
        {
            "name": "Narecor",
            "label": "Erlotinib2"
        }
    ]
}

DW Code

%dw 1.0
%output application/xml
---
response: {
 medication : payload.medication, 
 genericequivalents: { 
  (payload.genericequivalents map {
    names : {
     label: $.label,
     name: $.name 
    }
  }) 
 } 
} 

Use Case Two

Input

{
    "medication": "praxel",
    "genericequivalents": {
        "name": [
            "Praxe",
            "Praxedes",
            "Paxel",
            "Pragel",
            "Fraxel",
            "Parexel"
        ]
    }
}

DW Code


%dw 1.0
%output application/xml
---
{
 response: {
  medication: payload.medication,
  genericequivalents: {
   (payload.genericequivalents.name map ((name , indexOfName) -> {
    name: name
   }))
  }
 }
}

Output


   praxel
   
      Praxe
      Praxedes
      Paxel
      Pragel
      Fraxel
      Parexel
   

2 comments:

  1. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work..

    Mulesoft online training bangalore

    ReplyDelete
  2. Thank you for sharing .The data that you provided in the blog is informative and effective.mulesoft training in bangalore

    ReplyDelete

Anypoint Studio Error: The project is missing Munit lIbrary to run tests

Anypoint Studio 7.9 has a bug. Even if we following the article: https://help.mulesoft.com/s/article/The-project-is-missing-MUnit-libraries-...