Misalkan saya memiliki payload berikut dengan array bersarang, bagaimana cara menggabungkan array di dalam array bersarang untuk externalId yang sama serta beberapa logika pada bidang tertentu seperti

ShipQty - bidang ini akan dijumlahkan atau ditambahkan untuk catatan dengan externalId yang sama di bawah fillingOrder

SerialNumbers - semua catatan di bawah serialNumbers akan ditampilkan bersama jika externalId sama

Silakan lihat di bawah untuk input dan output yang diharapkan

Masukan Payload Json

     {
        "Identifier": "9i098p-898j-67586k",
        "transactionDate": "2019-09-08T10:01:00-04:00",
        "order": [
            {
                "orderNumber": "123456789",
                "CourierOrderId": "1300-88-2525",
                "fillingOrder": [
                    {
                        "numberOfBoxes": 0,
                        "tracking": [
                            {
                                "carrier": "Orange",
                                "trackNum": "3333444",
                                "trackUrl": "https://www.orange.com/track/status",
                                "shipDate": "2019-09-08T10:01:00-04:00",
                                "SerialNumber": "00000123"
                            }
                        ],
                        "row": [
                            {
                                "externalId": "1",
                                "unitNo": "OP04-123456-789",
                                "shipQty": 2,
                                "serialNumbers": [
                                    {
                                        "serialNumber": "USD333555",
                                        "quantity": 1
                                    },
                                    {
                                        "serialNumber": "USD235678",
                                        "quantity": 1
                                    }
                                ]
                            }
                        ]
                    },
                    {
                        "tracking": [
                            {
                                "carrier": "Apple",
                                "trackNum": "555666",
                                "trackUrl": "https://www.apple.com/track/status",
                                "shipDate": "2019-09-08T10:01:00-04:00",
                                "SerialNumber": "00000645"
                            }
                        ],
                        "row": [
                            {
                                "externalId": "1",
                                "unitNo": "OP04-123456-789",
                                "shipQty": 3,
                                "serialNumbers": [
                                    {
                                        "serialNumber": "USD123456",
                                        "quantity": 1
                                    },
                                    {
                                        "serialNumber": "USD98765",
                                        "quantity": 1
                                    },
                                    {
                                        "serialNumber": "USD45689",
                                        "quantity": 1
                                    }
                                    
                                ]
                            }
                        ]
                    },
                    {
                        "tracking": [
                            {
                                "carrier": "banana",
                                "trackNum": "587390",
                                "trackUrl": "https://www.banana.com/track/status",
                                "shipDate": "2019-09-08T10:01:00-04:00",
                                "SerialNumber": "00000365"
                            }
                        ],
                        "row": [
                            {
                                "externalId": "2",
                                "unitNo": "OP05-123456-111",
                                "shipQty": 2,
                                "serialNumbers": [
                                    {
                                        "serialNumber": "USD00045",
                                        "quantity": 1
                                    },
                                    {
                                        "serialNumber": "USD00046",
                                        "quantity": 1
                                    }
                                    
                                ]
                            }
                        ]
                    }
                ]
            }
        ]
    }

Output Json yang Diharapkan

{
    "row": [
        {
            "externalId": "1",
            "unitNo": "OP04-123456-789",
            "shipQty": 5, //the shipQty should be add up when the externalId is same
            "serialNumbers": [ //the serialNumbers should display all the data inside the serialNumbers when the externalId is same
                {
                    "serialNumber": "USD333555",
                    "quantity": 1
                },
                {
                    "serialNumber": "USD235678",
                    "quantity": 1
                },
                {
                    "serialNumber": "USD123456",
                    "quantity": 1
                },
                {
                    "serialNumber": "USD98765",
                    "quantity": 1
                },
                {
                    "serialNumber": "USD45689",
                    "quantity": 1
                }
            ]
        },
        {
            "externalId": "2",
            "unitNo": "OP05-123456-111",
            "shipQty": 2,
            "serialNumbers": [
                {
                    "serialNumber": "USD00045",
                    "quantity": 1
                },
                {
                    "serialNumber": "USD00046",
                    "quantity": 1
                }
        }
    ]
}
1
Jason Lee 7 Januari 2021, 12:23

3 jawaban

Jawaban Terbaik

Sepertinya Anda hanya memerlukan data "baris" di dalam bidang FillOrder dari payload Anda. Jadi hal pertama untuk menyederhanakan masalahnya adalah mendapatkan semua baris sebagai array tunggal. Setelah Anda memilikinya, Anda hanya perlu mengelompokkannya dengan id eksternal dan masalahnya akan mulai terlihat lebih kecil.

%dw 2.0
output application/json

//First get all rows since it looks like you only need them. 
//If you find this confusing try to use flatten with some simpler payloads. 
var allRows = flatten(flatten(payload.order.fillingOrder).row)

//Group them according to external id.
var groupedExtId = allRows groupBy $.externalId
---
{
    row: groupedExtId pluck ((value, extId, index) -> do {
        var sumShipQuant = sum(value.shipQty default [])
        ---
        {
            externalId: (extId), //the key after grouping is external id
            unitNo: value.unitNo[0], //assuming it is same across diff external id
            shipQty: sumShipQuant,
            serialNumbers: flatten(value.serialNumbers) //Flatten because value is an array and it has multiple serielNumbers array
        }
    })
}
1
Harshank Bansal 8 Januari 2021, 21:11

Ini harus membantu. Saya mengambil beberapa inspirasi dari posting Harshank Bansal

%dw 2.0
output application/json
var groupFlat = flatten(flatten (payload.order.fillingOrder).row) groupBy ($.externalId)
---

row: [groupFlat mapObject ((value, key, index) -> {
    externalId: value.externalId[0],
    unitNO: value.unitNo[0],
    shipQty: sum(value.shipQty),
    serialNumbers: flatten(value.serialNumbers)
})]
1
fpbg 10 Januari 2021, 15:01

Coba ini:

%dw 2.0
output application/json
---
row:[ if (payload..order..row..externalId[0] == payload..order..row..externalId[1]) {
externalId :  payload..order..row..externalId[0],
unitNo: payload..order..row..unitNo[0],
shipQty: payload..order..row..shipQty[0] + payload..order..row..shipQty[1],
serialNumbers:  flatten (payload..order..row..serialNumbers)  
}
else null]
0
fpbg 7 Januari 2021, 18:56