I'm struggling to transform XML code into JSON

Hi everyone, this is the first time I’ve had to use XML in n8n, and I thought the conversion to JSON would be easier. With the following workflow, I tried to do it, without success.

However, using the following site, the code transformed itself without any particular manipulation.

Can you give me some advice?

Here is my workflow:

Hi there, I believe you are missing the Move Binary Data Node between the Set and XML. You have a String that is pretending to be an XML file, but the XML node is looking for the file, not a string. Here is an example I use for parsing WordPress XML files. The first node pulls the binary file from my files:

I’ve just tried again by adding the missing node, but unfortunately it didn’t work :confused:. The XML code will always come from an HTTP node, via an API that I’ll call. The code snippet is a simulation of the result to be obtained. Unfortunately I can’t read a file on the n8n server.

Can you include a screenshot of the error the nodes are throwing?

Hi @arthurcorre,
could you double-check the validity of the XML?

I did a test with another XML and it works correctly:

Let me know if that helps!
best

Hi @djangelic, this is what I get as an error.

Hey @giulioandreini I have the impression that the code isn’t valid. But on the other hand, what I don’t understand is that by pasting it here in an abrupt way: https://codebeautify.org/xmltojson# it comes out correctly in JSON and the site manages to interpret it.

Hey @arthurcorre,

I think the issue here is that the XML node doesn’t take binary data as an input, Can you share what is in the JSON input?

1 Like

Hi @Jon, I let you run the workflow just below, normally I’ve taken care to integrate the XML code snippet into the Set node by copying and pasting the workflow.

Hi @arthurcorre , I’ve been playing with your sample and I’m having trouble figuring out what it could be. I’ve run your XML through 2 validators and it does seem like something is wrong with your XML output. It almost seems like it’s being truncated somehow.

Can you pass your XML through a validator like https://www.xmlvalidation.com and see if it gives you any errors?

1 Like

Hi @djangelic I’ve finally found the solution!!!

It seems that the file wasn’t valid. It was missing some closing tags that I’ve framed in red on the screenshot. I also ticked the ‘Keep Source’ option in the Move Binary Data node, which solved the problem completely. I hope this helps others :slight_smile:

3 Likes

Wonderful! Glad to hear it!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.