0

我正在尝试在 mule 4 dataweave 2.0 中将 json 转换为 csv 格式。但它不起作用。我们应该在 dataweave 中添加什么特定于 csv 格式的内容吗?我尝试在我的 dw 写作中:

    %dw 2.0
    output application/csv
    ---
    payload

请在我的 json 输入文件下面找到:

{"exportId": "WakeupHierarchy",
    "instance": "20190821151943036",
    "content": [
        {
            "file": {
                "header": {
                    "id": "12",
                    "name": "WakeupHierarchy",
                    "description": "Wakeup Hierarchy",
                    "version": "1.0"
                },
                "body": {
                    "records": [{
                            "alternateHierarchy": "ACD",
                            "tablePath": "/root/account/Account",
                            "referenceDataForeignKey": { "linked_record": {}},
                            "accountForeignKey": {
                                "linked_record": {
                                    "id": 100199493,
                                    "identification": {
                                        "sapId": "100091984",
                                        "name": "CHILDRENS HOSPITAL & MEDICAL CENTER"},
                                    "address": {
                                        "line1": "8200 DODGE ST",
                                        "line2": "ACCOUNTS PAYABLE" },
                                    "functions": {
                                        "commercialCustomer": true,
                                        "shipTo": true },
                                    "otherProperties": {
                                        "status": "A",
                                        "sapOrderBlockStatus": null},
                                    "hierarchies": {
                                        "memberships": []},
                                    "affiliations": {
                                        "shipTo": [],
                                        "billTo": []},
                                    "accountTBD": { "credit": {"creditAccount": null},
                                        "franchise": {"franchiseActivity": null },
                                        "tbdGroup": {
                                            "vcRebateProgram": null,
                                            "vcOrSurgicalBasedOnProductPurchasingVcSgOrBoth": null }},
                                    "technicalData": {
                                        "lastUser": "edie.belzner@alcon.com",
                                        "linkedTable": "/root/account/Account"}}},
                               "technicalData": {
                                "lastUser": "ed.com",
                                "primaryKey": "A93"}},
                         { "alternateHierarchy": "ACD",
                           "tablePath": "/root/account/Account",
                            "referenceDataForeignKey": {
                                "linked_record": {}},
                            "accountForeignKey": {
                                "linked_record": {
                                    "id": 19,
                                    "identification": {
                                        "sapId": null,
                                        "sfdcId": "803827"},
                                    "address": {
                                        "line1": "2601 S MCKENZIE ST STE 234",
                                        "line2": null},
                                    "functions": {
                                        "commercialCustomer": false,
                                        "shipTo": false},
                                    "otherProperties": {
                                        "status": null,
                                        "sapOrderBlockStatus": null},
                                    "hierarchies": {
                                        "memberships": []},
                                    "affiliations": {
                                        "shipTo": [],
                                        "billTo": []},
                                    "accountTBD": {"credit": {"creditAccount": null},
                                        "franchise": {"franchiseActivity": null},
                                        "tbdGroup": {
                                            "vcRebateProgram": null,
                                            "vcOrSurgicalBasedOnProductPurchasingVcSgOrBoth": null}},
                                    "technicalData": {
                                        "lastUser": "edie.belzner@alcon.com",
                                        "linkedTable": "/root/account/Account" }}}'                                
                            "technicalData": {
                                "lastUser": "edie@hotmail",
                                "primaryKey": "AC|19"}}]},
                "footer": {
                    "nb_records": 87
                }}}],
    "pagination": {"nextPage": "51943036/2"}}
4

1 回答 1

3

嗨,为了让 DW 编写 CSV,输出 DataStructure 需要是一个对象数组,所以为了做到这一点,您需要将输入的形状转换为对象数组,其中对象需要是简单值(字符串|数字|布尔|日期)

例如,您输入的一些简单内容是

%dw 2.0
output application/csv
---
flatten(payload.content.file.body.records) map ((record, index) -> 
    {
        alternateHierarchy: record.alternateHierarchy,
        tablePath: record.tablePath         
    }
)

这将输出

alternateHierarchy,tablePath
ACD,/root/account/Account
ACD,/root/account/Account
于 2019-08-22T13:26:58.187 回答