cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Transform multiple arrays into multiple records

manohar
Contributor

Hi there
I have following json

[   
 { 
    "quantity": [
      "3.0",
      "4.0",
      "2.0"
    ],
    "order-header-id": [
      "16737",
      "16737",
      "16737",
    ],
    "line-num": [
      "1",
      "2",
      "3",
    ],
    "coupalineAttacments": [
      {
        "id": {
          "@type": "integer",
          "$": "1878040"
        },
        "text": "OIL"
      },
      {
        "id": {
          "@type": "integer",
          "$": "1878042"
        },
        "text": "SURGE TRAP "
      }
    ]
  }
]

that I need to convert it into multiple records based on line-num

[
	{
		"quantity" : "3.0",
		"order-header-id": "16737",
		"line-num": "1",
      	"coupalineAttacments": 
      		{
        		"id": {
          			"@type": "integer",
          			"$": "1878040"
        		},
        		"text": "OIL"
      		}
      }
	{
		"quantity" : "4.0",
		"order-header-id": "16737",
		"line-num": "2", 
      	"coupalineAttacments": 
      		{
        		"id": {
          			"@type": "integer",
          			"$": "1878042"
        		},
        		"text": "SURGE TRAP "
      		}
      }
	{
		"quantity" : "2.0",
		"order-header-id": "16737",
		"line-num": "3",
		"coupalineAttacments": ""
      }
]

I have tried mapValues also map but doesnt look I am doing right. any guidance.

thanks
Manohar

2 REPLIES 2

AleksandarAngel
Contributor III

Hello @manohar,

The following expression should do the job:

sl.range(0, $quantity.length).map( x => $.mapValues(v => x < v.length ? v[x] : ""))

Let me know if this helps you.

BR,
Aleksandar.

Hi @AleksandarAngelevski, thank you very much. that helped.

Manohar