There is another solution for this without using script mediator, you can add

<?xml-multiple?> 

processing instruction to your payload. as follows;

<users>
    <?xml-multiple?>
    <user>user1</user>
</users>

this will create json array for users.

{"users": {"user": ["user1"]}}

Hope this will helpful.

Answer from jay on Stack Overflow
🌐
Newtonsoft
newtonsoft.com › json › help › html › ConvertXmlToJsonForceArray.htm
Convert XML to JSON and force array
string xml = @"<person id='1'> <name>Alan</name> <url>http://www.google.com</url> <role>Admin1</role> </person>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string json = JsonConvert.SerializeXmlNode(doc); Console.WriteLine(json); // { // "person": { // "@id": "1", // "name": "Alan", // "url": "http://www.google.com", // "role": "Admin1" // } // } xml = @"<person xmlns:json='http://james.newtonking.com/projects/json' id='1'> <name>Alan</name> <url>http://www.google.com</url> <role json:Array='true'>Admin</role> </person>"; doc = new XmlDocument(); doc.LoadXml(xml); json = JsonConvert.SerializeXmlNode(doc); Console.WriteLine(json); // { // "person": { // "@id": "1", // "name": "Alan", // "url": "http://www.google.com", // "role": [ // "Admin" // ] // } // }
Discussions

[XmlToJson] Single Element to JSON array Error | OutSystems
I've been encountering this problem where an element from the Xml can either have one or more items inside it, so when I convert it into Json: · One item translates to { "name" : "value" } while multiple items translate to [ { "name" : "value" } ] More on outsystems.com
🌐 outsystems.com
java - XML to JSON having an option for arrays with one element - Stack Overflow
Is there any way to convert XML string into JSON having an option for the arrays with one element? More on stackoverflow.com
🌐 stackoverflow.com
c# - Convert XML to Json Array when only one object - Stack Overflow
I am currently using Newtonsoft to convert some xml to json to return from a RestExtension. My xml is in the form of name More on stackoverflow.com
🌐 stackoverflow.com
Convert XML to Json Array when only one object Java - Stack Overflow
Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams · Get early access and see previews of new features. Learn more about Labs ... 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 ... More on stackoverflow.com
🌐 stackoverflow.com
April 19, 2021
🌐
GitHub
github.com › stleary › JSON-java › issues › 330
Having issue while convertnig xml with single element to json array or vice versa · Issue #330 · stleary/JSON-java
March 20, 2017 - but, we need similar behavior while converting json to xml and back again where json array is having single element i.e. input json - {"readResult": {"errors": [{"code": 400}]}} converted xml - <readResult><errors><code>400</code></errors></readResult> output json - {"readResult":{"errors":{"code":400}}} Reactions are currently unavailable · No one assigned ·
Author   rvashishth
🌐
OutSystems
outsystems.com › forums › discussion › 82490 › xmltojson-single-element-to-json-array-error
[XmlToJson] Single Element to JSON array Error | OutSystems
I've been encountering this problem where an element from the Xml can either have one or more items inside it, so when I convert it into Json: · One item translates to { "name" : "value" } while multiple items translate to [ { "name" : "value" } ]
🌐
Stack Overflow
stackoverflow.com › questions › 19183124 › xml-to-json-having-an-option-for-arrays-with-one-element
java - XML to JSON having an option for arrays with one element - Stack Overflow
I use org.json to convert XML into JSON at a Java code: JSONObject jSONObject = org.json.XML.toJSONObject(StringXML); The problem with org.json libs are they don't accept array with one element, ...
🌐
Cloud-elements
docs.cloud-elements.com › home › convert-xml-repeated-elements-json-array
Convert an XML with Repeated Elements to a JSON Array | Cloud Elements Help Center
March 23, 2020 - In the above XML, <GLENTRY> is repeated more than once and the JSON conversion is as shown below. The JSON converter converts the XML into a JSON array with multiple objects. However, if the XML contains only one repeated element, the JSON converter converts the XML into an object instead of ...
Find elsewhere
🌐
Apigee
community.apigee.com › questions › 4062 › xml-to-json-policy-single-array-element.html
Solved: XML to JSON policy single array element - Google Cloud Community
October 2, 2015 - I believe you can able to convert response payload using javascript policy available in Apigee Platform. For more details look at @Mike Dunker Article here which explains "Converting between XML and JSON".
Top answer
1 of 7
2

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 method 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 ;)

2 of 7
1

Based on Mario's solution, I made some changes, specially for nested children in the Json object. The xmlJSONObj is the same, but the obj param is a List that contains the path to the JSON child, and the index param is only required for the recursion (it should start at 0 in the first execution).

/**
       * Forces a JSON Object to be an Array. When converting from XML to JSON, There may be cases in
       * which we want a list of elements in the final JSON but there is only one element in the XML
       * file, so the XML.toJSONObject method will fail. This methods forces a JSON element to be an
       * array instead of just a JSON Object.
       * 
       * @param xmlJSONObj obtained by passing the XML through the method toJSONObject
       * @param obj list of String that contains the path to the JSON child to convert to Array
       * @param index required for recursion, starts at 0
       * @throws org.json.JSONException
       */
      public static void forceToJSONArray(JSONObject xmlJSONObj, List<String> obj, int index)
          throws org.json.JSONException {
        Object myObj = xmlJSONObj.opt(obj.get(index));
        if (myObj instanceof JSONObject && obj.size() == index + 1) {
          JSONObject myJSONObj = (JSONObject) myObj;
          JSONArray jsonArray = new JSONArray();
          jsonArray.add(myJSONObj);
          xmlJSONObj.put(obj.get(index), jsonArray);
        } else if (myObj instanceof JSONObject) {
          forceToJSONArray((JSONObject) myObj, obj, index + 1);
        }
      }

Example:

We have this JSON:

{
   "person":{
      "name":"Tony",
      "data":{
         "car":"Toyota"
      }
   }
}

And we want this JSON:

{
   "person":{
      "name":"Tony",
      "data":{
         "car":["Toyota"]
      }
   }
}

We should call the method like this:

forceToJSONArray(xmlJSONObj, Arrays.asList("person", "data", "car"), 0);
🌐
GitHub
github.com › stleary › JSON-java › issues › 235
XML to json Array Issue · Issue #235 · stleary/JSON-java
June 2, 2016 - 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. ... 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).
Author   smtrad
🌐
Axway
support.axway.com › kb › 177763 › language › en
KB Article #177763
March 6, 2016 - * When converting XML to JSON, an array with a single element is made into a simple value, rather than an array.
🌐
Microsoft Learn
learn.microsoft.com › en-us › archive › msdn-technet-forums › 03fe9c57-b7a8-41aa-ade4-652b6b5eafc3
Single element XML to JSON Array | Microsoft Learn
Yes this is one of the hiccups with XML to JSON transformation universially. We have an @array() function to help do this - if you pass in an array, we will return the array. If you pass in a single object, we will return an array with single object inside.
🌐
XML.com
xml.com › pub › a › 2006 › 05 › 31 › converting-between-xml-and-json.html
Converting Between XML and JSON
Now we apply the patterns above to convert this XML document fragment to a JSON structure. The outer list with two list items is converted using pattern 6. The first list item contains a single textual content "Subject 1" and an inner list element.
🌐
mibuso.com
forum.mibuso.com › nav tips & tricks
XML to JSON forcing XMLNode to be an Array — mibuso.com
October 30, 2018 - BEGIN ArrayNode := COPYSTR( TreatAsArray, 1, STRPOS(TreatAsArray,';')-1); TreatAsArray := COPYSTR( TreatAsArray, STRPOS(TreatAsArray,';')+1, STRLEN(TreatAsArray)); XMLNode := XmlDocument.SelectSingleNode( ArrayNode); XMLNode.Attributes.Append( XmlAtt); END; ArrayNode := TreatAsArray; XMLNode := XmlDocument.SelectSingleNode( ArrayNode); // xpath notation XMLNode.Attributes.Append( XmlAtt); END; Json := JSONConvert.SerializeXmlNode(XmlDocument.DocumentElement,JSONFormatting.Indented,TRUE); end; ... Hi Remco, I handled similar situation earlier, may be my idea help you. I don't have example ready with me.
🌐
SAP Community
community.sap.com › t5 › technology-q-a › get-xml-element-in-json-array-using-xml-o-json-converter › qaq-p › 11948873
Get XML element in JSON array using XML o JSON con... - SAP Community
May 21, 2019 - It only does so when the occurrence on the XML is more than 1. If you need to "force" the accounts field to be a JSON array even when there is only 1 element inside it, then you need to use a custom XML-to-JSON conversion.
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-xml-to-json-array-in-java
How to convert XML to JSON array in Java?
Jackson is a third-party Java library developed by FasterXML. It is used for converting Java objects to JSON and vice versa. To know more about Jackson, refer to the Jackson tutorial. We can use the XmlMapper class of the Jackson library to convert XML to JSON.
🌐
Yenlo
yenlo.com › home › wso2 enterprise integrator › from xml to json, how to handle an array of one
From XML to JSON, how to handle an array of one - Yenlo
April 25, 2025 - Or is it an array with one entry? There is no way to tell the difference, is there? … Well in fact there is, by including an annotation. StAXON supports the processing instruction. By adding this processing instruction to the XML, StAXON knows it should create an array for this lonely element.
🌐
Stack Overflow
stackoverflow.com › questions › 62722232 › xml-to-json-conversion-with-arrays
XML to JSON conversion with arrays - Stack Overflow
July 3, 2020 - For the above xml i have used the below XSLT code to convert into json. <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns0="http://use your namespace"> <xsl:output method="text"/> <xsl:template match="/ns0:Account_Resp">{ <xsl:apply-templates select="*"/> } </xsl:template> <!-- Object or Element Property--> <xsl:template match="*"> "<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/> </xsl:template> <!-- Array Element --> <xsl:template match="*" mode="ArrayElement"> <xsl:call-template name="Prope