json-to-list - Work with JSON Data (DEPRECATED)

This feature is deprecated. It was used actively in IM 1.18.x and older. It is kept now only for compatibility reasons.

This component is mainly used for loading PFX events in the JSON format. The loaded records can be resent directly to another PFX component (e.g. listToCsv) or can call an existing Camel endpoint. The attribute id is required. The component first converts JSON to XML and then XML to Java object.

XML creation could be defined in two ways:

  1. Simple one level flat structure when all elements are printed to values and if subelements exist, they are printed to a single string as well. All elements from JSON are added to the result. 

  2. Conversion with XSL file supplement in the parameter xsltTemplate where the subset of elements can be specified. As you can use your own root element and array structure element name in the XSL file, you can specify names of those elements in the parameters rootName and elementName which will be used during unmarshalling of JSON.

OutputUri is used inside the splitter which means that each row is processed separately and the route pointed in outputUri receives a message Map<Object,Object>. After that, all records are aggregated into a single message which could be e.g. stored to a file.

We found an error in Camel XML splitter. When there are more array elements in JSON (which are replaced by the elementName attribute), we call split for this elementName. The problem is then that pairing does not work properly and even if you have 2 open elements, it takes the first close element and totally messes up the XML. You get this error:

org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 542; XML document structures must start and end within the same entity.

The solution is to write your own xsltTemplate which removes duplicates of elementName in the XML structure to make split work properly.

 

Attributes

Description

Required?

Attributes

Description

Required?

id

ID of the component used for the route name.

yes

outputUri

Target route of the PFX component where the result will be sent.

no

rootName

Root name used in XSLT for conversion from JSON. The default is "root".

no

elementName

First level array element name used in XSLT. The default is "item".

no

batchSize

Size of the batch.

no

completionTimeout

Timeout.

no

xsltTemplate

Path to the .xslt file; a relative path starts in the /resource directory.

no

 

Example of a JSON event structure:

{ "data": [ { "version": 234, "sku": "1309915", "label": "1.081-131.0 SE 4001 SPRĂśHEXTRAKTIONSREINIGER", "resultPrice": 254.95000, "alerts": "{}", "warnings": "{}", "allowedOverrides": "attribute39,", "calculatedResultPrice": 254.95000, "tainted": false, "priceGridId": 184, "approvalState": "APPROVED", "typedId": "1837498.PGI", "createDate": "2016-02-05T06:12:15", "lastUpdateDate": "2016-05-06T09:02:14", ... } ], "metricName": "PGI_Approved", "eventType": "ITEM_APPROVED_PGI" }

 

Example of XSLT. Here, an array in the data element will get the name specified in elementName="event" and the name specified in rootName="root" will be used as the root element; the element data will be preserved.  The code takes only elements from JSON with the names label, priceGridId, resultPrice, sku and attribute1.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <event> <xsl:for-each select="root/data/event"> <label><xsl:value-of select="label"/></label> <priceGridId><xsl:value-of select="priceGridId"/></priceGridId> <resultPrice><xsl:value-of select="resultPrice"/></resultPrice> <sku><xsl:value-of select="sku"/></sku> <myCustomAttribute><xsl:value-of select="attribute1"/></myCustomAttribute> </xsl:for-each> </event> </xsl:template> </xsl:stylesheet>

 

Example of use in camel-context.xml with a combination of listToCsv component. The resulting XML created from JSON will be transformed to List<Map<[elementName],[value]>> where the list contains all first level array structure elements of the original JSON, e.g. "event".

<pfx:jsonToList id="jsonToListCsv" rootName="root" elementName="event" xsltTemplate="data/transformCsv.xsl"> <pfx:listToCsv delimiter=";" outputUri="mock:result" /> </pfx:jsonToList> <camelContext xmlns="http://camel.apache.org/schema/spring"> <route> <from uri="direct:startCsv"/> <to uri="direct:jsonToListCsv"/> <log message="DONE"/> </route> </camelContext>

IntegrationManager version 5.8.0