How to force an JSON object to display as an array

I am working to convert some xml to json .EX:For the below

John Doe Anna Smith Peter Jones

I convert this XMl by converting to document and then to JSON

I get this - an array of items in json (which is what I need):

{“employees”:{“employee”:[{“firstName”:“John”,“lastName”:“Doe”},{“firstName”:“Anna”,“lastName”:“Smith”},{“firstName”:“Peter”,“lastName”:“Jones”}]}}

But when there is only one it quite understandably converts like this (not an array):

{“employees”:{“employee”:{“firstName”:“John”,“lastName”:“Doe”}}}

My app developer who is reading this needs theJSON to return an array of items regardless or whether there is one or more.

Is there a way of tricking it into thinking it’s an array or can someone suggest another way of doing this?

If pub.json:documentToJSONString gets a document with a list as element it generates an array, even if there is ony one element in it. Your problem probably is in the transformation to the document from the xml. Please have a look into the generated document using debugger, I assume you won’t have a list there if only one element is present. The fields arrays and makeArrays of pub.xml:xmlNodeToDocument allow for explicit control where to generate arrays.

Hello,

Try out as suggestd below:

get xmlf file
pub.xml:xmlStringToXMLNode
pub.xml:xmlNodeToDocument – initialise makeArrays=true
pub.json:documentToJSONString

Also test the code with makeArrays=false (You must set makeArrays to false when using documentTypeName to define the structure of an element. Otherwise, an exception will be thrown at run time.)

Hello,

Try out as suggestd below:

get xmlf file
pub.xml:xmlStringToXMLNode
pub.xml:xmlNodeToDocument -- initialise makeArrays=true
pub.json:documentToJSONString

Also test the code with makeArrays=false (You must set makeArrays to false when using documentTypeName to define the structure of an element. Otherwise, an exception will be thrown at run time.)