I need to convert a json to xml and later on converting that json back to xml, but i am loosing the json array object while this conversion - I am using org.json lib.
Json specification says that json can have datatypes like string,number, boolean, array, object and null.
single element array in json is a special case which gives problem with some api like org.json. Because these library treat single element array as normal json object and not an json array object.
To solve this problem we need a library which support all the datatypes of a Json. We can use xslt transformation to solve this problem. In XSLT 3.0 there are 2 funtions json-to-xml() and xml-to-json() which works perfect for this problem. I have solved similar problem using this way. These functions transform json to xml and vice versa considering all the data type of json. for eg. In a json if you give value as number, boolean or string library will maintain these data types at the time of converstion (xml to json or vice versa so that after conversion in xml you can see particular datatype of a value in genrated xml). We can see in genrated xml that any key in json is always string type.
More on below link xslt 3.0 json-to-xml and xml-to-json conversion
Check both conversion examples on below xsltfiddle links
https://xsltfiddle.liberty-development.net/6qVRKvS
https://xsltfiddle.liberty-development.net/94hvTyU
Below is a complete java example of this conversion using XSLT and Saxon 9.8 HE (HE means home edition. It's open source product provides implementations of XSLT (3.0)
First you need to run json2XML.java which will genrate outputSampleXML.xml. This outputSample.XML will have conversion from json to xml and it will be input of our next program named xml2json.java. This program will give you back your original json containing single array element.
net.sf.saxon
Saxon-HE
9.8.0-8
com.google.code.gson
gson
2.8.2
{
"Request": [{
"price": "1400",
"test": "dummydata"
}]
}
public class json2XML {
public static void simpleTransform(String sourcePath, String xsltPath, String resultDir) {
TransformerFactory tFactory = TransformerFactory.newInstance();
try {
Transformer transformer = tFactory.newTransformer(new StreamSource(new File(xsltPath)));
transformer.transform(new StreamSource(new File(sourcePath)), new StreamResult(new File(resultDir)));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
simpleTransform("src/main/java/sampleJSON.xml", "src/main/java/json2xml.xsl",
"src/main/java/outputSampleXML.xml");
System.out.println("Done!!!");
}
}
public class json2XML {
public static void simpleTransform(String sourcePath, String xsltPath, String resultDir) {
TransformerFactory tFactory = TransformerFactory.newInstance();
try {
Transformer transformer = tFactory.newTransformer(new StreamSource(new File(xsltPath)));
transformer.transform(new StreamSource(new File(sourcePath)), new StreamResult(new File(resultDir)));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
simpleTransform("src/main/java/sampleJSON.xml", "src/main/java/json2xml.xsl",
"src/main/java/outputSampleXML.xml");
System.out.println("Done!!!");
}
}
{
"Request": [{
"price": "1400",
"test": "dummydata"
}]
}
Note you can keep both the xsl file in a java String object as a constant and use them as inputStream in transformer java code. So that you won't need to make seprate xsl files. But that is other part of problem and need not to concentrate here.