1
votes

I have below input

{
  "error1": [
    {
      "store": "1201",
      "DocumentNumber": "FF15974433790786634518"
    },
    {
      "store": "1201",
      "DocumentNumber": "FF15974433790786634519"
    },
    {
      "store": "1201",
      "DocumentNumber": "FF15974433790786634520"
    },
    {
      "store": "1202",
      "DocumentNumber": "FF15974433790786634520"
    }
  ],
  "error2": [
    {
      "store": "1201",
      "DocumentNumber": "FF15974433790786634518"
    }],
  error3:[]
}

i want to export following result

{
    "error1": [{
            "store": "1201",
            "DocumentNumber": ["FF15974433790786634518", "FF15974433790786634519", "FF15974433790786634520"]
        },
        {
            "store": "1202",
            "DocumentNumber": ["FF15974433790786634520"]
        }
    ],
    "error2": [{
        "store": "1201",
        "DocumentNumber": ["FF15974433790786634518"]
    }]
}
2
i got result with following script. may i know do we have alternate simple method. bcs i felt my code is too complicated fun mapper(payloadItem:Array) = (payloadItem groupBy $.storeCode) pluck(value,key) -> {"storeCode": (key), "salesOrderNumber" : value.salesDocumentNumber} --- (error filterObject ((value)->sizeOf(value)!=0)) mapObject { ($$): mapper($) }maomifadacai

2 Answers

2
votes

Try with this:

Input:

{
  "error1": [
    {
      "store": "1201",
      "DocumentNumber": "FF15974433790786634518"
    },
    {
      "store": "1201",
      "DocumentNumber": "FF15974433790786634519"
    },
    {
      "store": "1201",
      "DocumentNumber": "FF15974433790786634520"
    },
    {
      "store": "1202",
      "DocumentNumber": "FF15974433790786634520"
    }
  ],
  "error2": [
    {
      "store": "1201",
      "DocumentNumber": "FF15974433790786634518"
    }],
  "error3":[]
}

Script:

%dw 2.0
output application/json
--- 
payload filterObject ($ !=[]) mapObject {
     ($$) : $ groupBy $.store mapObject {
            temp:{
                store: $.store[0],
                DocumentNumber: $.DocumentNumber
            
}} pluck($)
}

Output:

{
  "error1": [
    {
      "store": "1201",
      "DocumentNumber": [
        "FF15974433790786634518",
        "FF15974433790786634519",
        "FF15974433790786634520"
      ]
    },
    {
      "store": "1202",
      "DocumentNumber": [
        "FF15974433790786634520"
      ]
    }
  ],
  "error2": [
    {
      "store": "1201",
      "DocumentNumber": [
        "FF15974433790786634518"
      ]
    }
  ]
}
1
votes

Try the below script

%dw 2.0
output application/json
---
(payload filterObject ($ != [])) mapObject ((value, key, index) -> {
    (key): (value groupBy $.store) pluck ((iValue, iKey) -> {
        store: (iKey),
        DocumentNumber: iValue.DocumentNumber
    })
})