APICALL

Hello,


I'm trying to setup some columns with some values like this exemple:


"price_lines": [

{

"price": "159.65",

"rate": 0.19,

"title": "MwSt"

},

{

"price": "123.65",

"rate": 0.17,

"title": "Tax2"

}

{

"price": "12.65",

"rate": 0.17,

"title": "Tax2"

}

]


price_1

price_2

price_3

159.65123.65
12.65

How can I do this?

If I use ${addColumns(row, c['price_lines'], "price")} I only got the last column.


Thank you.


Best regards,


Nicolas

Assuming the following JSON (based on your example but with the outer json object wrapper)


{
"price_lines" : [{
        "price": "159.65",
        "rate": 0.19,
        "title": "MwSt"
    },
    {
        "price": "123.65",
        "rate": 0.17,
        "title": "Tax2"
    },
    {
        "price": "12.65",
        "rate": 0.17,
        "title": "Tax2"
    }]

}


Parsing Code:


<#assign row = target.addRow()>
<#list json as j >
<#assign row = target.addRow()>
${addColumns(row, j["price_lines"][0], "price1_")}
${addColumns(row, j["price_lines"][1], "price2_")}
${addColumns(row, j["price_lines"][2], "price3_")}
</#list>




In this case you have to address the specific prices in the array explicitly.


Another more generic approach is this:


<#assign row = target.addRow()>
<#list json as j >
  <#assign row = target.addRow()>
  ${addColumns(row, j["price_lines"], "price_")}
</#list>


Which will give you this:

In this case you can have N number of prices. You could then split to get a specific price in a SpreadsheetMapper later:


e.g.

${price_price!?split(","[1])} to get the second price.


Thank you very much this is exactly what I need, but I have an error with


<#assign row = target.addRow()> <#list json as j > <#assign row = target.addRow()> ${addColumns(row, j["price_lines"][0], "price1_")} ${addColumns(row, j["price_lines"][1], "price2_")} ${addColumns(row, j["price_lines"][2], "price3_")} </#list>



Error during configuration of step 'SpreadsheetMapper': Second parameter (the node) must not be null. ---- FTL stack trace ("~" means nesting-related)


Any idea?


The second approach work fine but I would like to use the first one.


Thanks

It looks like there are not always 3 prices. E.g. when you do ${addColumns(row, j["price_lines"][2], "price3_")} but there is no 3rd price, then this parameter is null.

This is what the error is telling you.


You could try wrapping each line in an additional check:


<#if j["price_lines"][0]??>
${addColumns(row, j["price_lines"][0], "price1_")}
</#if>

<#if j["price_lines"][1]??>
${addColumns(row, j["price_lines"][1], "price2_")}
</#if>

etc.