DataMapper - Transform JSON array to XML

One Star

DataMapper - Transform JSON array to XML

Hi,
I use a mapping (component tHMap) to transform data from a JSON to XML.
However, I do not know how to retrieve the first, second (or more) values of a JSON array.
My first step would be to just get the first item and place it in the first tag category_1.
Ditto for the next elements.
"category" field into my JSON file:
"category":


Here is the structure that I want to generate:
<categories>
<category_1>test</category_1>
<category_1>test_talend_2</category_1>
<category_1>test_talend</category_1>
</categories>


Here is my mapping (Map):

I think we should to use a Loop function but i dont know how to use it:

Properties of "category" in JSON structure:

My second step is to sort the values by string length. Exemple:
<categories>
<category_1>test</category_1>
<category_1>test_talend</category_1>
<category_1>test_talend_2</category_1>
</categories>

Is there a function for this in DataMapper?
Thks all.
Moderator

Re: DataMapper - Transform JSON array to XML

Hello,
We have re-directed your issue to TDM expert and then come back to you as soon as we can.
Best regards
Sabrina
--
Don't forget to give kudos when a reply is helpful and click Accept the solution when you think you're good with it.
Employee

Re: DataMapper - Transform JSON array to XML

Hello PM_param, what's your expected output?

a loop of category_1?
<categories>
<category_1>test</category_1>
<category_1>test_talend_2</category_1>
<category_1>test_talend</category_1>
</categories>

category_1 and category_2 and category_3?
<categories>
<category_1>test</category_1>
<category_2>test_talend_2</category_2>
<category_3>test_talend</category_3>
</categories>

a sorted loop of category_1?
<categories>
<category_1>test</category_1>
<category_1>test_talend</category_1>
<category_1>test_talend_2</category_1>
</categories>

Something else?
Employee

Re: DataMapper - Transform JSON array to XML

To get the 2nd type of output, please use the AgConcat and SingleIndex functions as below
One Star

Re: DataMapper - Transform JSON array to XML

Hi hnie,
Thank's for your answer, the expected ouput is the lowest value in category 1, category 2 in the second and last (the longuest value) in category 3.
Today i resolve the first problem which was the XML structure. Now the field is set as below:

In my Map, I loop on "category" and I recovers only one that interested me as below:

However I think there is a simpler method to get the right values, no? There is a function for compare values length?
Thank's for you help.
Employee

Re: DataMapper - Transform JSON array to XML

Functions/String/StringLength and Functions/Comparison and Logical/* will allow your to compare values length.
One Star

Re: DataMapper - Transform JSON array to XML

Thank's for reply, I used a RegEx for determine category to use.
category_1:

category_2:
category_3:
However, when I click on "Test Run", I have the bug:
 "Exception during test run:

Thread Expr in$/Root/hits/hits/_source/category enclosing node: out$/Root/RequiredRecord/souscategorie_value != out$/Root/RequiredRecord/souscategorie2_value"
I specify that I use this function in several Maps. On the other Map (1 only), everything is ok.
There is a problem with json structure?