on ‎2019 Feb 01 3:28 AM
Hi Gurus!!!
I'm having problems with describe a java code to convert Xml to Json.
i had saw any codes in blogs, but all this codes has problem with Array.
this is my goal:

but my java code is generating this Json:

Someone, can help me with any idea how can I create this code correctly? I'll Appreciate it!!
Request clarification before answering.
you can use the external jar JSONObject.jar. I have given a sample code which is taking two test nodes under a root node and converting it into JSON Array -
package com.sap.XML2JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.XML;
public class XML2JSONArray
{
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public static String TEST_XML_STRING =
"<?xml version=\"1.0\" ?><root><test attrib=\"jsontext\">abctest</test><test attrib=\"jsontext2\">deftest</test></root>";
public static void main(String[] args) {
try {
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
String jsonPrettyPrintString = xmlJSONObj.toString(PRETTY_PRINT_INDENT_FACTOR);
System.out.println(jsonPrettyPrintString);
} catch (JSONException je) {
System.out.println(je.toString());
}
}
}
and here is the output -
{"root": {"test": [
{
"attrib": "jsontext",
"content": "abctest"
},
{
"attrib": "jsontext2",
"content": "deftest"
}
]}}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Sugata!!
I've the same code that you create.
package Xml2Json;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.StringWriter;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.json.JSONObject;
import org.json.XML;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import com.sap.aii.mapping.api.AbstractTransformation;
import com.sap.aii.mapping.api.StreamTransformationException;
import com.sap.aii.mapping.api.TransformationInput;
import com.sap.aii.mapping.api.TransformationOutput;
public class MakeItJSON extends AbstractTransformation
{
public static int PRETTY_PRINT_INDENT_FACTOR = 4;
public MakeItJSON() {}
public void transform(TransformationInput in, TransformationOutput out) throws StreamTransformationException
{
try {
String sourcexml = "";
String targetfile = "";
String line = "";
InputStream ins = in.getInputPayload().getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
while ((line = br.readLine()) != null) {
sourcexml = sourcexml + line;
}
JSONObject jsonObject = XML.toJSONObject(sourcexml);
String jsonPrettyPrintString = jsonObject.toString(PRETTY_PRINT_INDENT_FACTOR);
targetfile = jsonPrettyPrintString;
out.getOutputPayload().getOutputStream().write(targetfile.getBytes());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
but if i put more "items" the json code not create all the items.

Hi,
Although this is an old question, yet would like to answer it.
To achieve the required JSON format you have 2 alternatives:
1) Install a custom adapter and use it on the Rest receiver channel as mentioned here.
2) Use a custom Java mapping to convert the XML in the desired JSON structure as mentioned here.
Regards,
Kiran Roy
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I've found the problem!!!
Thanks for your support!!!
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Can you please share your code?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
| User | Count |
|---|---|
| 12 | |
| 9 | |
| 6 | |
| 4 | |
| 4 | |
| 4 | |
| 3 | |
| 3 | |
| 2 | |
| 2 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.