One Star

Force mapping as array in Rest json request / tXMLMap

Hi,
I need to create a REST request in json format with an array of a second result in it.
My job works so far in 2 msqlinputs -> tXMLMap -> RESTClient (see screenshot 2)
The tXMLMap maps and joins to 2nd result of tMSSQLInput_2 to a loop item (lineItems - see screenshot 1) which is interpreted correctly as the formatting to json at the REST request does it as array - but only if there are at least 2 items in the tMSSQLInput_2 result.
If there is only 1 item in the resultset "lineItems" is formatted as object.
Unfortunately the REST server always expects an array there - no matter if there is only 1 item or more.
What do I need to do that "lineItems" are always formatted as array? Or do I need an other way of formatting the json?
Thanks in advance

  • Data Integration
10 REPLIES
Community Manager

Re: Force mapping as array in Rest json request / tXMLMap

Hi
It is impossible to force the json format from component level if there is only one item, if allowed, you can duplicate the item or generate another false item if there is only one item, so that the component can interpreted correctly the required json format.
Shong
----------------------------------------------------------
Talend | Data Agility for Modern Business
Employee

Re: Force mapping as array in Rest json request / tXMLMap

It appears to be a well-known Jettison related issue, as it does not know whether a single element represents a collection/array or not. Note in this case we have an auto XML to JSON conversion at tRESTClient level which is where Jettison is used.
We need to get it fixed.
One Star

Re: Force mapping as array in Rest json request / tXMLMap

Hi
I run into a similar problem when trying to to do a REST call with JSON.
Is there already a solution or workaround for the collection/array problem available. Duplication - as mentioned in a post above - is no option.
I have to use both, single element and array, for different elements in the same JSON, so I would appreciate a solution where I can choose single elemet or array at element level.
Thx in advance.
One Star

Re: Force mapping as array in Rest json request / tXMLMap

I solved it with a regular expression in a tMap object:
StringHandling.EREPLACE(row8.myout,"lineItems\"\\:\\{(.*)\\}","lineItems\":")
row8.myout is the already rendered json string. "lineItems":{*some stuff inside*} will be replaced to "lineItems":
One Star

Re: Force mapping as array in Rest json request / tXMLMap

Hi,
thanks fpr your reply. Nice workaround. But as I use a tXmlMap and tRestClient, i've got a xml document in Talend, which is transformed to json by the tRestCLient component. So i don't have a json string before sending.
One Star

Re: Force mapping as array in Rest json request / tXMLMap

I use tWriteJSONField now instead of tXMLMap, so you still can do something with the output. Maybe you have to do this convertion, too. (parsing your xml to readable data first)
One Star

Re: Force mapping as array in Rest json request / tXMLMap

I am encountering the same issue. Its been two years since this issue has been reported. Any updates from Talend team? 
Thanks
Harsha
Employee

Re: Force mapping as array in Rest json request / tXMLMap

I'd like to provide some info on the implementation. The auto-conversion is managed by a CXF JSON provider which is based on Jettison. Jettison uses XML stream events to convert XML tags into JSON. Unfortunately due to a lack of the type information Jettison is unable to understand that a given single element represents a single element array, it is only able to guess it if more that 1 element with the same name is available. Technically Jackson is more capable but it depends on annotations to do the XML->JSON auto-conversion right which are not easy to set up in the studio context.
CXF and Jettison offer the configuration options that can help Jettison always treat a given element as an array. Please vote for resolving
https://jira.talendforge.org/browse/TESB-11120 
One Star

Re: Force mapping as array in Rest json request / tXMLMap

Looks like the Issue TESB-11120 has been moved or does not exist. Any ideas for a work around?
Thanks
Harsha
Employee

Re: Force mapping as array in Rest json request / tXMLMap

I've created a publicly visible issue:
https://jira.talendforge.org/browse/TESB-16205.
Sergey