1

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"]
    }]
}
1
  • 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($) } Commented Dec 31, 2020 at 8:39

2 Answers 2

2

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
    })
})
Sign up to request clarification or add additional context in comments.

Comments

2

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"
      ]
    }
  ]
}

4 Comments

thanks. i don't know how to iterate in dataweave, so i had to use "fun mapper(payloadItem:Array) = (payloadItem groupBy $.storeCode) pluck(value,key) -> {"storeCode": (key), "salesOrderNumber" : value.salesDocumentNumber} " to get item
one more question, can i use "DocumentNumber: $.DocumentNumber" instead of DocumentNumber: $.*DocumentNumber. seems there have same output.
thanks for pointing that out.. It probably spilled over from a different approach that i was working on for this solution. I will edit the answer with the update.
As well just a suggestion, please do put code snippets in an answer block vs comments as they can be structure to be more readable in the former style as compared to a bit clumsy in the latter.. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.