Manipulating JSON and XML with Node.js

In this entry, we will describe how to handle JSON and XML formats within node.js applications. Such formats are involved when using REST or simply when exchanging data through ASCII protocols.

Using JSON

The JSON format is natively supported by Node.js. Nothing needs to be added and no module inserted using require. Converting JSON to object and object to JSON can be done using the JSON object and its parse and stringify methods.

Following code describes how to generate a string containing the JSON content for a JavaScript object.

var contact = {
id: 1,
lastName: "last name",
firstName: "first name"
};
var text = JSON.stringify(contact);
// contains {"id":1,"lastName":"last name","firstName":"first name"}

Following code describes how to get JavaScript object from a string containing the JSON content.

var text = "{\"id\":1,\"lastName\":\"last name\","
     +"\"firstName\":\"first name\"}";
var contact = JSON.parse(text);

Using XML

XML support isn’t built-in and you need to add additional module (see https://github.com/joyent/node/wiki/modules#parsers-xml). The libxmljs module provides a wrapper for the libxml2 library. For this purpose, we need to install following libraries:

  • libxml2: the XML library itself
  • libxml2-dev: developement tools regarding the libxml2 library
  • scons: tool regarding building

Don’t forget to install libxml2-dev since the SConstruct file uses the xml2-config one provided by the package to build the nodejs module.

Under Debian/Ubuntu, you can simply execute following command to install all of them:

sudo apt-get install libxml2 libxml2-dev scons

Some documentations regarding the libxmljs module can be reached at the address https://github.com/polotek/libxmljs/blob/master/docs/Home.md.

Following code describes how to generate a string containing the XML content for a XML object.

var document = new libxmljs.Document(function(n) {
  n.node("contact", function(n) {
    n.node("id", "1");
    n.node("lastName", "last name");
    n.node("firstName", "first name");
  });
});

this.xml.toString();
// contains <contact><id>1</id><lastName>last name</lastName>
// <firstName>first name</firstName></contact>

Following code describes how to get XML object from a string containing the JSON content.

var text = "<contact><id>1</id><lastName>last name</lastName>"
    +"<firstName>first name</firstName></contact>";

var xml = libxmljs.parseXmlString(text);
var contactElement = xml.root();
var idElement = contactElement.childNodes()[0];
var id = idElement.childNodes()[0];
var lastNameElement = contactElement.childNodes()[1];
var lastName = lastNameElement.childNodes()[0];
var firstNameElement = contactElement.childNodes()[2];
var firstName1 = firstNameElement.childNodes()[0];

About these ads
This entry was posted in JavaScript, Node.js and tagged , , , , . Bookmark the permalink.

4 Responses to Manipulating JSON and XML with Node.js

  1. ntop says:

    hi,your article really help me a lot ,but when i run
    ‘ sudo apt-get install libxml2 libxml2-dev scons’
    on ubuntu ,it go in wrong with error message-
    ‘libxml2-dev: depends: libxml2 (= 2.7.8.dfsg-2) but 2.7.8.dfsg-2ubuntu0.1 about to be installed’

    but i can’t install 2.7.8.dfsg-2ubuntu0.1 one is powerpc only,can you help me solve the problem?

  2. ntop says:

    hi,it seems i have solved the problem after i removed the libxml2 and re-installed it.
    Thank you all the same!

  3. Hi there! This article couldn’t be written any better!
    Looking at this article reminds me of my previous roommate!
    He always kept preaching about this. I am going
    to forward this article to him. Fairly certain he’s going
    to have a good read. Many thanks for sharing!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s