emerog
emerog

Reputation: 79

Convert and Transform JSON HTTP request to XML

I need to create a Logic Apps workflow with three steps:

  1. When HTTP Request is received (JSON)
  2. Convert Json from request to XML
  3. Save XML file to FTP

What I have done so far:

  1. Add action "When HTTP Request is received"
  2. Add Liquid to Convert JSON to XML (but i don't see option JSON to XML...Only Tranform JSON to JSON, JSON to TEXT, XML to JSON, XML to TEXT)
  3. Add action "FTP - Create file"

I also created Integration Account and try to add map for mapping JSON to XML, but I can't find any examples/templates to do this...

Is it possible at all ? Maybe there is another way to convert between these two formats ?

Upvotes: 1

Views: 3892

Answers (1)

jcools85
jcools85

Reputation: 236

When you just want to convert a JSON payload to an XML file, without doing any transformation to the data, you can use the built-in xml() function of the Workflow Definition Language.

Detailed info in the docs: Workflow Definition Language reference #xml

I've made a small test Logic App to demo your usecase. It looks like this:

enter image description here

As you can see I use the xml function on the triggerbody @xml(triggerBody()) as an input for my FTP file content.

Remark: This will only work if your JSON message has a single rootnode. Otherwise the xml conversion will fail. You'll get this error:

The provided value cannot be converted to XML: 'JSON root object has multiple properties. The root object must have a single property in order to create a valid XML document. Consider specifying a DeserializeRootElementName.

You can work around that by concatenating a rootnode to your JSON payload. The function then would look like: @xml(json(concat('{\"rootnode\":',triggerBody(),'}')))

Good luck testing this out. Let me know if you need more help with this.

Upvotes: 3

Related Questions