โ10-28-2021 02:33 AM
I have a requirement, where by I need to copy the same data into all the entries in a JSON array. The data I need to copy is
โcountryIsoโ : โGBโ
โsessionIdโ : โed7e8228-a82b-44e0-adea-caee6c61a987โ
I have an typical JSON array structure:
"orderLine": [
{
"orderLineNumber": "000004",
"product": "21909232109737",
"productName": "Avon WT7 Snow (Winter Tyre) 195/65 R15 T (91)",
"quantity": "48",
"unitPrice": "44.34"
},
{
"orderLineNumber": "000013",
"product": "039400014592",
"productName": "Goodyear Eagle F1 Asymmetric 5225/40 R18 Y (92)",
"quantity": "32",
"unitPrice": "38.88"
},
Then I want to copy countryIso and sessionId to each array entry, like this:
"orderLine": [
{
"orderLineNumber": "000004",
"product": "21909232109737",
"productName": "Avon WT7 Snow (Winter Tyre) 195/65 R15 T (91)",
"quantity": "48",
"unitPrice": "44.34",
"countryIso" : "GB",
"orderId" : "ed7e8228-a82b-44e0-adea-caee6c61a987"
},
{
"orderLineNumber": "000013",
"product": "039400014592",
"productName": "Goodyear Eagle F1 Asymmetric 5225/40 R18 Y (92)",
"quantity": "32",
"unitPrice": "11.88"
"countryIso" : "GB",
"orderId" : "ed7e8228-a82b-44e0-adea-caee6c61a987"
},
I have used extend to add the fields, but try as I might I just canโt copy the data to each entry, has anybody done anything similar?
Regards
Jay
โ10-28-2021 03:02 AM
Hey @Walkback,
Use the .map() method on $orderLine. The expression should look something like this:
$orderLine.map(x=>x.extend({"countryIso" : "GB","sessionId" : "ed7e8228-a82b-44e0-adea-caee6c61a987"}))
Hereโs also a sample pipeline:
Extend objects in array_2021_10_28.slp (3.9 KB)
Regards,
Bojan
โ10-28-2021 03:36 AM
Bojan
Thank you for your feedback, but I have already tried that. Iโm currently using a mapper snap (which maybe the wrong way to do it) used the data once, but there are after errors, because the 2 values are both null.
Is there a means of retaining the 2 values?
Jay
โ10-28-2021 03:52 AM
What should happen if the values are null? You can either filter those with a filter function:
$orderLine.filter(y=>y!=null)
or keep them as nulls with extending the expression with a ternary operator:
$orderLine.map(x=>x != null ? x.extend({"countryIso" : "GB","sessionId" : "ed7e8228-a82b-44e0-adea-caee6c61a987"}) : x)
โ10-28-2021 05:27 AM
Itโs not clear how your countryIso and sessionId fields enter the pipeline (are they constants, pipeline parameters?), but Iโve attached an example thatโs pretty simple and doesnโt involve javascript expressions. Uses a Splitter to break out the lines, then a mapper to add the countryIso and sessionId (orderId?), then a GroupBy to put it all back together. You can use a JSON formatter to spit it back out depending on what your are doing with it. Hope this gives you some ideas anyway.
walkbackCopyData_2021_10_28.slp (7.7 KB)