Convert XML to Json Array when only one object Java

xml to json converter java
how to convert xml file to json in java
convert xml with attributes to json java
convert xml cdata to json-java
convert json to xml with namespace in java
xml to json java program
convert xml to json using jackson example
xml to json keep order java

I have one XML which i need to convert to JSON when in XML we have more than one element its creating proper jsonArray but when single element not creating the array can any one please help me how we can get array in case of single element

String TEST_XML_STRING = "<Items><Item><Name>Stack</Name><Name>OverFlow</Name></Item></Items>";
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
// output - {"Items":{"Item":{"Name":["Stack","OverFlow"]}}}

when

String TEST_XML_STRING = "<Items><Item><Name>Stack</Name></Item></Items>";
JSONObject xmlJSONObj = XML.toJSONObject(TEST_XML_STRING);
// output - {"Items":{"Item":{"Name":"Stack"}}}

but it should be {"Items":{"Item":[{"Name":"Stack"}]}}

I'm late on this... but, using org.json:

public static void forceToJSONArray(JSONObject xmlJSONObj, String obj) throws org.json.JSONException 
{
    // where "JSONObject xmlJSONObj" is my JSONObject obtained by passing the XML throug the metod toJSONObject 
    // and where "String obj" is the name of the JSONObject I want to force to JSONArray
    Object myObj = xmlJSONObj.opt(obj);
    if (myObj == null)
    {
        // if my obj doesn't exist inside my xmlJSONObj i create it empty
        JSONArray jsonArray = new JSONArray();
        xmlJSONObj.put(obj, jsonArray);
    }
    else if ( myObj instanceof JSONObject ) 
    {
        // if exist but is a JSONObject, I force it to JSONArray
        JSONObject myJSONObj = (JSONObject)myObj;
        JSONArray jsonArray = new JSONArray();
        jsonArray.put(myJSONObj);
        xmlJSONObj.put(obj, jsonArray);
     }
     else if ( myObj instanceof String || myObj instanceof Integer ) 
     {
        // if exist but is a primitive entry (like in your case), I force it to a "primitive" JSONArray
        JSONArray jsonArray = new JSONArray();
        jsonArray.put(myObj);
        xmlJSONObj.put(obj, jsonArray);
     }  
}

Hope this could help ;)

How to convert XML to JSON array in Java?, We can convert XML to JSON array using org. json. XML class, this provides a static method, XML. toJSONObject() to convert XML to JSON array. I couldn't solve this. My issue is to convert a json object having array with single element when converted to xml, should indicated if that is an array/list instead of simple xml element. This api works fine when i have more then one entry in json array. But when i have only one entry in json array it don't.

If XML tag has a single child, it will convert this child to an object instead of an array. But if XML tag has 2+ children of the same name, it returns an array.

You can do nothing with this. The only way is to use another library.

Having issue while convertnig xml with single element to json array , http://heshans.blogspot.in/2014/01/java-library-to-convert-xml-to-json.html. I am getting But when i have only one entry in json array it don't. I am currently using Newtonsoft to convert some xml to json to return from a RestExtension. Convert XML to Json Array when only one object. Force JSON.NET to

There doesn't seem to be out of the box way of doing that. If the json is processed in the code later on you could check whether it's JSONObject or JSONArray. In case you need to return json object in the correct format from let say spring mvc rest controller, you could also check what you dealing with (JSONArray or JSONObject). When it's JSONObject you will have to replace it with JSONArray with the same data.

Test if it is JSONObject or JSONArray

XML to json Array Issue · Issue #235 · stleary/JSON-java · GitHub, I need to parse a specific XML element of xml content, that has only one occurency, to a JSON Array object, using XML.toJSONObject(xml). This sample reads the json:Array="true" attribute in the XML and places its value in an array when converting the XML to JSON.

If you are using Java 8 or later, you should check out my open source library: unXml. unXml basically maps from Xpaths to Json-attributes.

It's available on Maven Central.

Example

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.nerdforge.unxml.factory.ParsingFactory;
import com.nerdforge.unxml.parsers.Parser;
import org.w3c.dom.Document;

public class Parser {
  public ObjectNode parseXml(String xml){
    Parsing parsing = ParsingFactory.getInstance().create();
    Document document = parsing.xml().document(xml);

    Parser<ObjectNode> parser = parsing.obj("/Items/Item")
        .attribute("name", "Name")
        .build();

    ObjectNode result = parser.apply(document);
    return result;
  }
}

It will return a Jackson ObjectNode, with the following json:

{"name":"Stack"}

You can also create a parser that will return nested ObjectNodes or ArrayNodes to whatever JSON structure you desire.

Convert XML to JSON and force array, This sample reads the json:Array="true" attribute in the XML and places its value in an array when converting the XML to JSON. 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.

I use these two methods, but you will have to identify which fields you want to wrap in a array. With javax.json.*:

private static JsonObject wrapFieldInArray(JsonObject object, String fieldKey) {
    JsonValue fieldValue = object.get(fieldKey);
    if(fieldValue instanceof JsonObject){
        JsonObjectBuilder builder = Json.createObjectBuilder(object);
        builder.remove(fieldKey);
        builder.add(fieldKey, wrapObjectInArray((JsonObject) fieldValue));
        object = builder.build();
    }
    return object;
}

public static JsonArray wrapObjectInArray(JsonObject obj){
    JsonArrayBuilder builder = Json.createArrayBuilder();
    builder.add(obj);
    return builder.build();
}

And with org.json:

private static JSONObject wrapFieldInArray(JSONObject object, String fieldKey) {
    Object fieldValueObj = object.get(fieldKey);
    if(fieldValueObj instanceof JSONObject){
        JSONObject fieldValue = (JSONObject) fieldValueObj;
        object.remove(fieldKey);
        object.put(fieldKey, wrapObjectInArray(fieldValue));
    }
    return object;
}

public static JSONArray wrapObjectInArray(JSONObject obj){
    return new JSONArray().put(new JSONObject(obj.toString()));
}

XML to JSON policy single array element, array element. I am trying to convert XML to JSON using the XML to JSON policy. when there is only one employee this is converted to an object instead of an array: that could perhaps be a JSMapr JavaScrtipt. I just  Learning how to convert Java objects into JSON and JSON into Java will help you a lot in your work as a full stack mobile app developer. In this blog post I will share with you code examples which will help you do most of the tasks you will even need to do when working with JSON and Java.

Java Mapping - Convert Xml To Json - With Array, 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  XStream library can be an effective tool for converting JSON to Java to XML translations to and fro. Lets explore each one of them one by one, and see which driver is used. Handling JSONs To convert JSON to Java objects all you have to do is initialize XStream object with an appropriate driver and…

How to convert XML to JSON in Java, The only problem was with Array, I wanted the array to be in the same order as it and one of the requirement was to convert xml to json for one feed which was in xml format. public JSONObject(Map<String, Object> map) {. We were using mongodb to store the feed so I was looking for a simple solution. I came across many libraries and finally used json.org. Find here how to convert XML to JSON in Java. The only problem was with Array, I wanted the array to be in the same order as it was in the XML file. So I had to make a small modification to the JSONObject class.

Dealing with Single Value Arrays in JSON, A typical XML to JSON converter will convert this as follows: Note: This is a common problem with JAXB, a popular java XML binding the data type at runtime, and if necessary, converts it to a single element array The only thing I'​m really saying is that you name this a problem with JSON, I'd say it's a problem with XML. I made a choice to write java mapping for same because this adapter module only supports JSON to XML (my requirement is to decrypt first and do json to xml) Now, in Java mapping I have the code for decrypt, but after this I am unable to get the code for JSON to XML. If you can help with the proper jars and code , that helps. System: PI 7.4 dual

Comments
  • can you suggest another library?