- Saved searches
- Use saved searches to filter your results more quickly
- License
- lukas-krecan/json2xml
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- How to Convert JSON to XML or XML to JSON in Java
- Convert JSON to XML in Java
- Convert XML to JSON in Java
- 13 thoughts on “How to Convert JSON to XML or XML to JSON in Java”
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Java JSON to XML converter
License
lukas-krecan/json2xml
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
…maven.plugins-maven-javadoc-plugin-3.5.0 Bump maven-javadoc-plugin from 3.4.1 to 3.5.0
Git stats
Files
Failed to load latest commit information.
README.md
Java Json to XML conversion
Json2Xml project is a simple implementation of JSON to XML conversion. Under the hood it uses Jackson pull parser and generates XML SAX events. This way the conversion has low memory consumption and is pretty fast.
There is already Jettison project that has similar objective, unfortunately it can not handle JSON arrays properly.
Json2Xml converts the following JSON
< "document":< "a":1, "b":2, "c":< "d":"text" >, "e":[1, 2, 3], "f":[[1, 2, 3], [4, 5,6]], "g":null, "h":[ < "i":true, "j":false >], "k":[ [ ], [ , ] ] > >
1 2 text 1 2 3 1 2 3 4 5 6 true false 1 2 3 4 5
6
If you have SAX content handler, you can use net.javacrumbs.json2xml.JsonSaxAdapter class directly.
ContentHandler ch = . ; JsonSaxAdapter adapter = new JsonSaxAdapter(json, ch); adapter.parse();
Otherwise it’s possible to use net.javacrumbs.json2xml.JsonXmlReader together with standard Java transformation.
Transformer transformer = TransformerFactory.newInstance().newTransformer(); InputSource source = new InputSource(. ); Result result = . ; transformer.transform(new SAXSource(new JsonXmlReader(),source), result);
Transformer transformer = TransformerFactory.newInstance().newTransformer(); InputSource source = new InputSource(new StringReader(json)); DOMResult result = new DOMResult(); transformer.transform(new SAXSource(new JsonXmlReader(namespace, addTypeAttributes, artificialRootName), source), result); result.getNode();
Since XML does not have any mechanism to reflect JSON type information, there is a new feature since json2xml version 1.2. You can switch on the addTypeAttributes flag using a constructor argument. Then you will get the type information in XML attributes like this:
1 2 text 1 2 3 1 2 3 4 5 6 true false 1 2 3 4 5
6
XML support only one root element but JSON documents may have multiple roots. To overcome this mismatch, you can specify artificialRootName which will generate artificial XML root with given name. new JsonXmlReader(null, false, «artificialRoot») will transform
Converting XML DOM back to JSON
If you have created an XML DOM node from JSON content with addTypeAttributes then you can convert it back to JSON using net.javacrumbs.json2xml.JsonXmlHelper .
// convert JSON to DOM Node Node node = JsonXmlHelper.convertToDom(. ); // do whatever on the DOM Node (eventually modify using XPATH it while respecting the type attributes) String json = JsonXmlHelper.convertToJson(node);
This method has proven very useful to work on huge JSON document using XPATH and converting it back to JSON afterward.
Other difference between JSON and XML are allowed names. In cases, when your JSON contains names not allowed as XML element names, an ElementNameConverter can be used. For example to convert ‘@’ to ‘_’ create the following reader
new JsonXmlReader("", false, null, new ElementNameConverter() < public String convertName(String name) < return name.replaceAll("@","_"); >>)
Version 4.1 handles arrays differently than the previous version. The change is in handling of arrays of JSON objects. In older version it behaved erratically. Version 4 fixes the behavior, but is backwards-incompatible.
Also note that version 2.0 and higher require Jackson 2.x If you need new features in Jackson 1.x, just file a ticket and I will backport the changes.
Please do not use version 4.0. It has broken handling of arrays. Since it has been in the wild for two days only, I do not assume anyone is using it.
To use with Maven, add this dependency
net.javacrumbs json-xml 4.2 = 2.0 --> 1.3-->
How to Convert JSON to XML or XML to JSON in Java
In this tutorial you will learn how to convert json to xml or xml to json in java.
While programming we may come across a situation where we have to convert json to xml or vice versa. JSON.org provides a library that can be used for this conversion process in just one line of code.
Below I have shared examples to do this.
Download the json java library from below link and import in your project.
If you don’t know how to import a library in java then you can read below article.
Convert JSON to XML in Java
Just pass the given json object in XML.toString() method to convert it into xml data.
Convert XML to JSON in Java
Just pass the xml data into XML.toJSONObject() method to convert it into json object.
Programming Tip: Level up your programming skills by moving your software development/testing environment into the cloud with innovative hosted citrix vdi from CloudDeskopOnline with remote accessibility to essential programming tools on any device(PC/android/iOS). Looking for a dedicated server for your project? Try out dedicated gpu hosting and Enterprise E3 suite from Apps4Rent to gear up your work productivity.
13 thoughts on “How to Convert JSON to XML or XML to JSON in Java”
Is there any way to maintain order while transforming JSON to XML. I have a Input JSON file. But when I convert it into XML, the order will be changed. I was searching solution from past 2 days. I tried in mule as well as in Java but it won’t work. This is the Input JSON data.
“status” : “success”,
“response” : “p_status_msg” : “contact” : “contactlist” : [“address” : “90 Grove St., Newport ME 04953; United States of America”,
“grp” : “ADDRESS”,
“pidm” : “”,
“type” : “Home (preferred)”
>, “address” : “[email protected]”,
“grp” : “EMAIL”,
“pidm” : “”,
“type” : “Academy Email”
>, “address” : “[email protected]”,
“grp” : “EMAIL”,
“pidm” : “”,
“type” : “Email”
>, “address” : “(207) 341-3018”,
“grp” : “PHONE”,
“pidm” : “”,
“type” : “Cell Phone”
>
],
“contact” : “studentid” : “P000020581”,
“dateofbirth” : “07/07/1995”,
“firstname” : “John”,
“socialsecuritynumber” : “005961046”,
“gender” : “M”,
“middlename” : “Q.”,
“lastname” : “Adams”
>
>
>
>
>
Please help me out. Thanks.
Yes you can maintain the order.
Once the program gives you have the output result, copy and paste it a notepad and then cut paste the data to bring it in correct order.
Underscore-java library has methods xmlToJson and jsonToXml.