Node for converting JSON to XML?!

1,287 views
Skip to first unread message

jo2

unread,
Sep 23, 2015, 4:21:58 PM9/23/15
to Node-RED
Hi!

I need to convert some JSON to XML. Is there a Node-RED node for that, like for XML to JSON?!

I guess I can use this module, but takes some more work then...


Cheers
-jo

Nicholas O'Leary

unread,
Sep 23, 2015, 4:24:03 PM9/23/15
to Node-RED Mailing LIst
Hi,

the XML node goes both ways - give it a string and it'll parse it as XML to a javscript object. Give it a javascript object it attempts to build the XML representation of it.

Nick

--
http://nodered.org
---
You received this message because you are subscribed to the Google Groups "Node-RED" group.
To unsubscribe from this group and stop receiving emails from it, send an email to node-red+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jo2

unread,
Sep 24, 2015, 4:55:40 AM9/24/15
to Node-RED
Hi Nick!

I actually tried using the XML node for it, but got an error response.
The input was a JSON string, I now tried to parse it>
msg.payload = JSON.parse(msg.payload);

and then the XML node was happy about it.

I noticed that the response back to the client (a web browser in this case) was incomplete (cut off in the middle).
Whilst a console.log showed the correct and entire XML.

I added this to set the content length header before returning from the flow, and then the client received the entire XML.

msg.headers['Content-Length'] = Buffer.byteLength(msg.payload, ['utf8']);
msg.headers['Content-Type'] = 'text/xml; charset=UTF-8';


Cheers
-jo

Dave C-J

unread,
Sep 24, 2015, 12:05:08 PM9/24/15
to node...@googlegroups.com
Jo,

Yes when Nick said it converts a javascript object to XML he really meant an object :-)
Where are you getting the json from in the middle of this flow ? Is it another web call ? If so it may be that the content-length is already set to the length of the json response (which I guess would be a lot shorter than the xml equivalent). If so you can either set it explicitly like you have, or I think you can delete it and the http response node will add it if it doesn't exist... 

jo2

unread,
Sep 24, 2015, 3:27:44 PM9/24/15
to Node-RED

chez yess, I should have checked the node info tab better;-)

You are absolutely correct - the json comes from an external call earlier in the flow. So seems that Content-Length were carried on to the http response node.
I tried delete the msg.headers altogether, and also setting the Content-Type to null and 0, but did not seem to help. So I stick with the other setting.

How about the http response node calculates the correct content length of whatever is contained in msg.payload when the response node is reached?
Is the content JavaScript object, calculate the JSON length, otherwise, the string length. Maybe it is not that straightforward?

Cheers
-jo
Reply all
Reply to author
Forward
0 new messages