如何将 JSON 数组展平为 csv 文件

How do I flatten a JSON array to a csv file

我有一个 JSON 格式的传入有效负载,我将其中的一些 objects 输出到 CSV 文件。 payload也有一个数组:

      "Chargebacks": [
        {
          "CostCenterCode": "123ABC",
          "AllocationPercentage": 100
        },
        {
            "CostCenterCode": "456DEF",
            "AllocationPercentage": 100
        }
      ]

我需要 CSV 文件包含:

<other headers from the objects>,Cost Center Code 1, Allocation Percentage 1, Cost Center Code 2, Allocation Percentage 2
<other object values>,123ABC,100,456DEF,100

我的第一次尝试是创建两个变量来保存 headers 列表和值列表:

%dw 2.0
output application/csv
var x = payload.Item.CatalogAttributes.Chargebacks map (chargeBack, index) -> 
{
    "header": "Cost Center Code " ++ index+1 ++ ", Allocation Percentage "++ index+1,
    "costCenterCode": chargeBack.CostCenterCode ++ "," ++ chargeBack.AllocationPercentage,
}
var foo = x.*header joinBy ','
var bar = x.*costCenterCode joinBy ','
---

并将它们添加到文件末尾:

foo: bar

它有点管用。我在 headers 的末尾得到值“foo”,在值的末尾得到 123ABC\,100\,456DEF\,100 。如何获取 foo 的实际值并从值中删除斜杠?

在我的回答中,我假设您可能事先不知道有多少拒付项目。这个数据编织:

%dw 2.0
output application/csv
---
payload map {
    ($ - "Chargebacks"),
    ($.Chargebacks map {
        ("CostCenterCode_$($$)": $.CostCenterCode),
        ("AllocationPercentage_$($$)": $.AllocationPercentage)
    })
}

使用此示例输入:

[
    {
        "field1": "someValue",
        "field2": "someValue",
        "Chargebacks": [
            {
                "CostCenterCode": "123ABC",
                "AllocationPercentage": 100
            },
            {
                "CostCenterCode": "456DEF",
                "AllocationPercentage": 100
            }
        ]
    },
    {
        "field1": "someValue2",
        "field2": "someValue2",
        "Chargebacks": [
            {
                "CostCenterCode": "123ABC2",
                "AllocationPercentage": 200
            },
            {
                "CostCenterCode": "456DEF2",
                "AllocationPercentage": 200
            }
        ]
    }
]

生成此 csv:

field1,field2,CostCenterCode_0,AllocationPercentage_0,CostCenterCode_1,AllocationPercentage_1
someValue,someValue,123ABC,100,456DEF,100
someValue2,someValue2,123ABC2,200,456DEF2,200

通过将我们的地图包装在 (...) 中,我们基本上是在告诉它获取结果数组并将其展平到顶级对象中。如果您熟悉的话,这与 javascript 中的扩展运算符非常相似。 $$$ 是函数的 shorthand。例如,如果你有一个像这样的函数:fun someFun(left, fn: (item, index) -> Any),你可以使用这种模式 payload someFun ... 调用它,其中 payload 成为参数 left,然后右边成为函数;每个传入函数的参数变成一个$,其中$的个数就是参数的位置。说得通?请注意,这种调用函数的模式并不限于采用函数的模式。例如,您可以创建这样的函数:fun add(left, right) = left + right 并这样调用它 1 add 2。这仅在使用 fun 关键字且恰好有两个参数时有效。

如果您的尺寸可能不规则(即:有些尺寸可能比其他尺寸大)并且需要为较小的元素留出空白条目,您需要提前确定最大尺寸并执行像这样:

%dw 2.0
output application/csv
var maxSize = max(payload map sizeOf($.Chargebacks))
---
payload map (row) -> {
    (row - "Chargebacks"),
    ((1 to maxSize) map {
        ("CostCenterCode_$($$)"): row.Chargebacks[$$].CostCenterCode,
        ("AllocationPercentage_$($$)"): row.Chargebacks[$$].AllocationPercentage
    })
}

这将映射这样的输入:

[
    {
        "field1": "someValue",
        "field2": "someValue",
        "Chargebacks": [
            {
                "CostCenterCode": "123ABC",
                "AllocationPercentage": 100
            },
            {
                "CostCenterCode": "456DEF",
                "AllocationPercentage": 100
            },
            {
                "CostCenterCode": "456DEF",
                "AllocationPercentage": 100
            }
        ]
    },
    {
        "field1": "someValue2",
        "field2": "someValue2",
        "Chargebacks": [
            {
                "CostCenterCode": "123ABC2",
                "AllocationPercentage": 200
            },
            {
                "CostCenterCode": "456DEF2",
                "AllocationPercentage": 200
            }
        ]
    }
]

对此:

field1,field2,CostCenterCode_0,AllocationPercentage_0,CostCenterCode_1,AllocationPercentage_1,CostCenterCode_2,AllocationPercentage_2
someValue,someValue,123ABC,100,456DEF,100,456DEF,100
someValue2,someValue2,123ABC2,200,456DEF2,200,,