【问题标题】:Transform nested arrays into same level as parent in mulesoft dataweave将嵌套数组转换为与 mulesoft dataweave 中的父级相同的级别
【发布时间】:2021-09-12 19:00:32
【问题描述】:

我想将嵌套信息放入 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 mulesoft


    【解决方案1】:

    您可以尝试以下方法。如果您开始使用 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
    
         })
    }
    

    【讨论】:

    • 嗨,非常感谢。我可以与您核实如何获取每个下载网址并放入 Request HTTP 吗?
    • 如果这将是您在 http 调用之前的有效负载,那么您可以在 Http-Requestor 的 url 字段中使用 for-eachpayload.downloadUrl
    【解决方案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,,,,,
    

    【讨论】:

    • 嗨萨利姆,再次感谢,这很好,但我有的是,“水果”:“香蕉”,“颜色”:“黄色”,“质量”:[],有这个有“质量”,但里面什么都没有。输出还是一样,希望你能帮忙,非常感谢!
    • 您需要清楚地给出您的输入和预期输出。绝对不能在同一个问题中问5个问题。不仅仅是 Stackoverflow 的工作原理。
    • 对于混乱的信息,我深表歉意。希望您能帮助解决这个问题。
    • 更新了上面的答案以解决您的问题。
    • 请务必选择答案作为问题的解决方案,以便在其他人正在寻找类似问题的解决方案时帮助他们。理想情况下,答案应该是 Imtiaz 的解决方案,尽管您在同一个问题中不断修改您的问题(另一个不这样做的原因)。
    【解决方案3】:

    您应该将已编辑的部分作为一个新问题提出。但是,您编辑的问题的解决方案如下:

    输入

    {
        "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
    

    【讨论】:

    • 您好,谢谢。我可以与您核实一个 csv 中是否有超过 1 个水果吗?例如香蕉。所以香蕉的输出将是例如。 (附加行)282,香蕉,黄色,2021-09-12,好,4,正常,3
    • 因为目前 payload.topic[0].quality 给出了要查看的行号,但我想查看所有水果,而不是苹果的 [0] 和香蕉的 [1]
    【解决方案4】:

    输入

    {
        "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
    

    【讨论】:

    • 嗨,再次感谢。例如,如果香蕉没有“质量”,那么它根本不会显示香蕉排,是否也可以显示香蕉排?像'282,banana,yellow,,,,,',所以如果'quality'没有值,它将显示为空但仍然有香蕉行。因为使用上面的代码,它不会完全有香蕉行
    猜你喜欢
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-22
    • 2017-09-02
    相关资源
    最近更新 更多