cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Parsing XML Array to JSON array is inconsistent

mohamadelmardin
New Contributor III

When using XML parser snap to parse XML documents we are facing a problem when handling XML array type. This is because XML parser parses XML Array at runtime to either a JSON array if the particular element in the incoming XML message comes as array or as a single JSON object/element if the particular element in the incoming XML message comes as a single element not as an array. This is causing a major problem down the processing pipeline especially when using Mapper or any similar snaps where the parsed JSON data comes either as an array or as element {} and pipeline breaks for error.

The question is how can we force XML parser or combine it with any other technique so that it will parse XML element to JSON always as an array even if it comes as a single XML array element. Attached is an example of a simple pipeline reading an xml file with attached schema xsd. If the XML item element cames as an array then the pipeline will process successfully til the tend but if it comes as a single item then it will definitely fails at Mapper because JSON cannot treat it as an array
ParsingXMLArray_2017_07_24.slp (4.5 KB)
shippingtestingschema.xsd.txt (1.2 KB)
shiporder.xml.txt (673 Bytes)
shiporder_OneItem.xml.txt (527 Bytes)

8 REPLIES 8

Hi

I am new to SnapLogic. I need to use the Snap to parse XML file which has multiple elements and nested structure. Do you know where can I start to learn the XML parser or some Snaps? I found really limited resource available on Youtube, Google.

Regards,
Jim

First you have to use the out-of-box XML parser snap and configuring it should be easy as described in SnapLogic documentation. Then the hard part begins after the incoming XML input parsed into JSON is to navigate through the nested structure and map any array or non-existing elements. The best way to do that is to use a series of Mapper snaps and in each mapper you have to use expression language statements which is a subset of JavaScript. To read more about expression language please check the following link:
https://docs-snaplogic.atlassian.net/wiki/spaces/SD/pages/1438042/Expression+Language+Overview

For example to cast an parsed XML element in JSON into an array type you would use the following expression: [].concat[ element ]

Please refer also to the beginning of this thread for other examples.

Even I too also facing the same issue. while flattening the multiple structure( both single child and multiple child elements) xml source to individual columns to target tables

Could anyone find any optimal solution for this?

I am using JSON Splitter and mapper snap but unable to find optimal solution and in-consistent for different structure of xml.

Please update here if anyone find solution for this.

Thanks,
Surendar

debasish
New Contributor

Use XML schema validation in XML parser with schema file provided.
In the XSD schema lets have the child elements defined with minOccurs=โ€œ0โ€ as explicitly defined .
This will help you solve all the issues at all places.

โ€œE1EXCHANGE_RATEโ€: {
โ€œ@SEGMENTโ€: โ€œ1โ€,
โ€œLOG_SYSTEMโ€: โ€œXXXXXXXXโ€,
โ€œDEV_ALLOWโ€: โ€œ000โ€,
โ€œE1ABCDEF_0โ€: [
{
โ€œ@SEGMENTโ€: โ€œ1โ€,
โ€œRATE_TYPEโ€: โ€œ200*โ€
}
]
},

XSD:
<xs:element name=โ€œE1ABCDEF_0โ€ maxOccurs=โ€œunboundedโ€ minOccurs=โ€œ0โ€>
xs:complexType
xs:sequence
<xs:element type=โ€œxs:stringโ€ name=โ€œRATE_TYPEโ€ minOccurs=โ€œ0โ€/>