Jump to content

How to Convert XML to JSON in Java

+ 1
  tmo9d's Photo
Posted Sep 28 2009 08:52 AM

Use json-lib, a library which adds JSON support to any Java program. json-lib can take XML and convert it into a JSON object.

1. Add these dependencies to your project:
<dependency>
    	<groupId>net.sf.json-lib</groupId>
    	<artifactId>json-lib</artifactId>
    	<version>2.3</version>
    	<type>jar</type>
    	<classifier>jdk15</classifier>
    	<scope>compile</scope>
    </dependency>
    <dependency>
    	<groupId>org.apache.commons</groupId>
    	<artifactId>commons-io</artifactId>
    	<version>1.3.2</version>
    	<type>jar</type>
    	<scope>compile</scope>
    </dependency>
    <dependency>
    	<groupId>xom</groupId>
    	<artifactId>xom</artifactId>
    	<version>1.1</version>
    </dependency>
  </dependencies>


What does this mean? See How to Add a Dependency to a Java Project

2. Put the following XML sample in your classpath in a file named sample-xml.xml:
<?xml version="1.0" encoding="UTF-8"?>
<important-data certified="true" processed="true">
  <timestamp>232423423423</timestamp>
  <authors>
    <author>
      <firstName>Tim</firstName>
      <lastName>Leary</lastName>
    </author>
  </authors>
  <title>Flashbacks</title>
  <shippingWeight>1.4 pounds</shippingWeight>
  <isbn>978-0874778700</isbn>
</important-data>


3. Load the resource from the classpath and parse this XML into JSON as follows:
package com.discursive.answers;

import java.io.InputStream;

import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;

import org.apache.commons.io.IOUtils;

public class ConvertXMLtoJSON {

	public static void main(String[] args) throws Exception {
		InputStream is = 
			ConvertXMLtoJSON.class.getResourceAsStream("sample-xml.xml");
		String xml = IOUtils.toString(is);
		
		XMLSerializer xmlSerializer = new XMLSerializer(); 
		JSON json = xmlSerializer.read( xml );  
		System.out.println( json.toString(2) );
	}
}


4. This should produce the following JSON output:

{
  "@certified": "true",
  "@processed": "true",
  "timestamp": "232423423423",
  "authors": [  {
    "firstName": "Tim",
    "lastName": "Leary"
  }],
  "title": "Flashbacks",
  "shippingWeight": "1.4 pounds",
  "isbn": "978-0874778700"
}



This sample project is available on GitHub here: http://github.com/to...le-json-parsing

For more information about the json-lib project, see JSON-LIB project page

Java and XML

Learn more about this topic from Java and XML, 3rd Edition.

This new edition shows you how to cut through all the hype about XML and put it to work. You'll learn how to use the APIs, tools, and tricks of XML to build real-world applications. After two chapters on XML basics, the rest of the book focuses on using XML from your Java applications. The result is a new approach to managing information that touches everything from configuration files to web sites.

See what you'll learn


Tags:
1 Subscribe


1 Reply

 : Apr 08 2013 08:11 PM
This does not seem to work. What am I doing wrong?



Exception in thread "main" java.lang.NullPointerException
at java.io.Reader.<init>(Reader.java:78)
at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
at org.apache.commons.io.IOUtils.copy(IOUtils.java:1906)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:778)
at org.apache.commons.io.IOUtils.toString(IOUtils.java:759)
at ReadXml.main(ReadXml.java:40)



import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;
import org.apache.commons.io.IOUtils;

public class ReadXml {

public static void main(String[] args) throws IOException {

InputStream is = ReadXml.class.getResourceAsStream("h1.summary.xml");
String xml = IOUtils.toString(is);

XMLSerializer xmlSerializer = new XMLSerializer();
JSON json = xmlSerializer.read(xml);
System.out.println(json.toString(2));