Forum Discussion

Walkback's avatar
Walkback
New Contributor
4 years ago

How to copy data multiple times

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

5 Replies

  • Walkback's avatar
    Walkback
    New Contributor

    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

    • bojanvelevski's avatar
      bojanvelevski
      Valued Contributor

      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)

  • epearson's avatar
    epearson
    New Contributor III

    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)

  • Walkback's avatar
    Walkback
    New Contributor

    Thank you for your support and suggestions. I solved this problem, using a stored procedure.