1

I have an JSON like this:

{
"invoice-line": [
    {
        "id": "01",
        "date": "2019-06-21",
        "sales-date": "2019-06-21",
        "line-num": "1",
        "item": "Mouse",
        "descrption": "This is line 1 item"
    },
    {
        "id": "02",
        "date": "2019-06-21",
        "sales-date": "2019-06-21",
        "line-num": "2",
        "item": "Keyboard",
        "descrption": "This is line 2 item"
    },
    {
        "id": "03",
        "date": "2019-06-21",
        "sales-date": "2019-06-21",
        "line-num": "3",
        "item": "Monitor",
        "descrption": "This is line 3 item"
    }
]
}

From this input JSON we need to extract the data and map into another output JSON but with a logic:
when line-num = 1 then map id, date, sales-date, description then rest or the array will map id, item, descrption
Which will look like below:

{
"row": [
    {
        "id": "01",
        "date": "2019-06-21",
        "sales-date": "2019-06-21",
        "line-num": "1",
        "item": "",
        "descrption": "This is line 1 item"
    },
    {
        "id": "02",
        "date": "",
        "sales-date": "",
        "line-num": "",
        "item": "Mouse",
        "descrption": "This is line 2 item"
    },
    {
        "id": "03",
        "date": "",
        "sales-date": "",
        "line-num": "",
        "item": "Monitor",
        "descrption": "This is line 3 item"
    }
]
}

Also is that possible to perform something like mapping array item (first index) to different output array? For example:

{
"row": [
    {
        "id": "",
        "date": "2019-06-21",
        "sales-date": "2019-06-21",
        "line-num": "1",
        "item": "",
        "descrption": ""
    },
    {
        "id": "01",
        "date": "",
        "sales-date": "",
        "line-num": "",
        "item": "Keyboard",
        "descrption": "This is line 1 item"
    },
    {
        "id": "02",
        "date": "",
        "sales-date": "",
        "line-num": "",
        "item": "Mouse",
        "descrption": "This is line 2 item"
    },
    {
        "id": "03",
        "date": "",
        "sales-date": "",
        "line-num": "",
        "item": "Monitor",
        "descrption": "This is line 3 item"
    }
]
}

1 Answer 1

1

Hopefully this is what you were looking for:

%dw 1.0
%output application/json
---
row: 
    (payload.invoice-line filter ($.line-num == '1') map {
            id: "",
            date: $.date,
            sales-date: $.sales-date,
            line-num: $.line-num,
            item: "",
            descrption: ""
        })
    ++
    (payload.invoice-line map {
            id: $.id,
            date: "",
            sales-date: "",
            line-num: "",
            item: $.item,
            descrption: $.descrption
    })

produces:

{
    "row": [
        {
            "id": "",
            "date": "2019-06-21",
            "sales-date": "2019-06-21",
            "line-num": "1",
            "item": "",
            "descrption": ""
        },
        {
            "id": "01",
            "date": "",
            "sales-date": "",
            "line-num": "",
            "item": "Mouse",
            "descrption": "This is line 1 item"
        },
        {
            "id": "02",
            "date": "",
            "sales-date": "",
            "line-num": "",
            "item": "Keyboard",
            "descrption": "This is line 2 item"
        },
        {
            "id": "03",
            "date": "",
            "sales-date": "",
            "line-num": "",
            "item": "Monitor",
            "descrption": "This is line 3 item"
        }
    ]
}
Sign up to request clarification or add additional context in comments.

5 Comments

thank a lot, but yes, this isn't what i want as the output field (post target) is not controllable from my side so i cant add header or lines on that row array
No worries. I can get you the expected output tomorrow morning
Thanks, that's help me a lot...still looking for the method to apply two different logic to map on one array...
hopefully the new edit is better. If it is, please mark the answer as correct. Thanks!
this look great. Thanks for the help!

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.