将嵌套数组转换为与 mulesoft dataweave 中的父级相同的级别
Transform nested arrays into same level as parent in mulesoft dataweave
我想将嵌套信息放入 1 个父行。我是 mulesoft 的新手,希望得到指导。
输入:
{
"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"
}
} ]}] }
输出到 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"
谢谢。
您可以试试下面的方法。如果您刚开始使用 Dataweave,那么 DW playground 是一个学习的好地方。
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
})
}
您应该将编辑过的部分作为一个新问题提出。然而,您编辑的问题的解决方案如下:
输入
{
"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"
}
} ]}] }
脚本
%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
})
输出
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
输入
{
"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"
}
} ]}] }
脚本
%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))
输出
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
输入
{
"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"
} ] }
脚本
%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))
输出
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,,,,,
我想将嵌套信息放入 1 个父行。我是 mulesoft 的新手,希望得到指导。
输入:
{
"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"
}
} ]}] }
输出到 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"
谢谢。
您可以试试下面的方法。如果您刚开始使用 Dataweave,那么 DW playground 是一个学习的好地方。 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
})
}
您应该将编辑过的部分作为一个新问题提出。然而,您编辑的问题的解决方案如下:
输入
{
"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"
}
} ]}] }
脚本
%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
})
输出
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
输入
{
"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"
}
} ]}] }
脚本
%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))
输出
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
输入
{
"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"
} ] }
脚本
%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))
输出
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,,,,,