poppp
poppp

Reputation: 341

Transform nested arrays into same level as parent in mulesoft dataweave

I would like to put nested information into 1 parent row. I am new to mulesoft and would appreciate guidance.

input:

{
    "number": 282,
    "topic": [
        {
            "fruit": "apple",
            "colour": "red",
            "quality": [
                {
                    "date": "2020-08-21",
                    "in": {
                        "feedback": "good",
                        "qty": "3"
                    },
                    "out": {
                        "feedback": "poor",
                        "qty": "1"
                    }
                },
                {
                    "date": "2020-08-22",
                    "in": {
                        "feedback": "normal",
                        "qty": "0"
                    },
                    "out": {
                        "feedback": "good",
                        "qty": "2"
                    }
} ]}] }

output to csv:

"number", "fruit", "colour", "date", "feedback_in", "qty_in", "feedback_out", "qty_out"
"282",  "apple", "red", "2020-08-21", "good", "3", "poor", "1"
"282", "apple", "red", "2020-08-22", "normal", "0", "good", "2"

Thank you.

Upvotes: 0

Views: 404

Answers (4)

Salim Khan
Salim Khan

Reputation: 4303

Input

{
    "number": 282,
    "topic": [
        {
            "fruit": "apple",
            "colour": "red",
            "quality": [
                {
                    "date": "2020-08-21",
                    "in": {
                        "feedback": "good",
                        "qty": "3"
                    },
                    "out": {
                        "feedback": "poor",
                        "qty": "1"
                    }
                },
                {
                    "date": "2020-08-22",
                    "in": {
                        "feedback": "normal",
                        "qty": "0"
                    },
                    "out": {
                        "feedback": "good",
                        "qty": "2"
                    }
} ]},
{
            "fruit": "banana",
            "colour": "yellow"
} ] }

Script

%dw 2.0
output application/json

---
flatten(payload.topic map ((item) -> {
     temp: (if(sizeOf(item.quality)==0) [{}] else item.quality) default [{}] map {
     number: payload."number",
     fruit: item.fruit,
     colour: item.colour,    
      date:$."date" default "",
     feedback_in: $.in.feedback default "",
     qty_in: $.in.qty default "",
     feedback_out: $.out.feedback default "",
     qty_out: $.out.qty default ""
     }
}.temp))

Output

number,fruit,colour,date,feedback_in,qty_in,feedback_out,qty_out
282,apple,red,2020-08-21,good,3,poor,1
282,apple,red,2020-08-22,normal,0,good,2
282,banana,yellow,,,,,

Upvotes: 1

Salim Khan
Salim Khan

Reputation: 4303

Input

{
    "number": 282,
    "topic": [
        {
            "fruit": "apple",
            "colour": "red",
            "quality": [
                {
                    "date": "2020-08-21",
                    "in": {
                        "feedback": "good",
                        "qty": "3"
                    },
                    "out": {
                        "feedback": "poor",
                        "qty": "1"
                    }
                },
                {
                    "date": "2020-08-22",
                    "in": {
                        "feedback": "normal",
                        "qty": "0"
                    },
                    "out": {
                        "feedback": "good",
                        "qty": "2"
                    }
} ]},
{
            "fruit": "banana",
            "colour": "yellow",
            "quality": [
                {
                    "date": "2020-09-01",
                    "in": {
                        "feedback": "better",
                        "qty": "2"
                    },
                    "out": {
                        "feedback": "ok",
                        "qty": "1"
                    }
                },
                {
                    "date": "2021-01-02",
                    "in": {
                        "feedback": "bad",
                        "qty": "0"
                    },
                    "out": {
                        "feedback": "ugly",
                        "qty": "2"
                    }
} ]}] }

Script

%dw 2.0
output application/csv
---
flatten(payload.topic map ((item) -> {
     temp: item.quality map {
     number: payload."number",
     fruit: item.fruit,
     colour: item.colour,    
      date:$."date",
     feedback_in: $.in.feedback,
     qty_in: $.in.qty,
     feedback_out: $.out.feedback,
     qty_out: $.out.qty
     }
}.temp))

Output

number,fruit,colour,date,feedback_in,qty_in,feedback_out,qty_out
282,apple,red,2020-08-21,good,3,poor,1
282,apple,red,2020-08-22,normal,0,good,2
282,banana,yellow,2020-09-01,better,2,ok,1
282,banana,yellow,2021-01-02,bad,0,ugly,2

Upvotes: 1

Salim Khan
Salim Khan

Reputation: 4303

You should have asked the edited part as a new question. However the solution to your edited ask is below:

Input

{
    "number": 282,
    "topic": [
        {
            "fruit": "apple",
            "colour": "red",
            "quality": [
                {
                    "date": "2020-08-21",
                    "in": {
                        "feedback": "good",
                        "qty": "3"
                    },
                    "out": {
                        "feedback": "poor",
                        "qty": "1"
                    }
                },
                {
                    "date": "2020-08-22",
                    "in": {
                        "feedback": "normal",
                        "qty": "0"
                    },
                    "out": {
                        "feedback": "good",
                        "qty": "2"
                    }
} ]}] }

Script

%dw 2.0
output application/csv
---
payload.topic[0].quality map ({
     number: payload.number,
     fruit: payload.topic[0].fruit,
     colour: payload.topic[0].colour,
     date:$."date",
     feedback_in: $.in.feedback,
     qty_in: $.in.qty,
     feedback_out: $.out.feedback,
     qty_out: $.out.qty
     })

Output

number,fruit,colour,date,feedback_in,qty_in,feedback_out,qty_out
282,apple,red,2020-08-21,good,3,poor,1
282,apple,red,2020-08-22,normal,0,good,2

Upvotes: 1

Imtiyaz Qureshi
Imtiyaz Qureshi

Reputation: 281

You can try something like below. If you are getting started with Dataweave then DW playground is a great place to learn. https://developer.mulesoft.com/learn/dataweave/

%dw 2.0
output application/csv
---
do {
     var id = payload.id
     var topic = payload.topic
     var password = payload.password
     ---
     payload.recording_files map ((item) -> {
         id: id,
         topic: topic,
         recordId: item.id,
         recordType: item.file_type,
         downloadUrl: item.download_url,
         password: password

     })
}

Upvotes: 2

Related Questions