从json中排除一些已经映射到一些键中的键值

时间:2018-10-31 12:27:23

标签: mule dataweave

在映射一组属性时,我需要从输出中删除一些字段。

输入:

[
    {
        "eventType": "xxx",
        "entityId": "xxxxxx",
        "userName": "xxxxx",
        "dateTime": "2010-11-11T07:00:00Z",
        "status": "SHIPPED",
        "additionalData": {
            "quoteId": "xxxxx",
            "clientReferenceId": "Srites004",
            "modifiedDt": "2010-11-11T07:00:00Z",
            "packageId": "AIM_PACKAGE",
            "sbsOrderId": "TEST-TS-201809-79486",
            "orderReferenceId": "xxxxxx",
            "shipDate_1": "2010-11-11T07:00:00Z",
            "shipDate_2": "2010-11-12T07:00:00Z",
            "shipDate_3": "2010-11-13T07:00:00Z",
            "shipMethod_1": "UPS Ground",
            "shipMethod_3": "UPS Ground3",
            "shipMethod_2": "UPS Ground2",
            "trackingNumber_3": "333",
            "trackingNumber_1": "2222",
            "trackingNumber_2": "221"
        }
    }
]

输出:

{
    "attribute_name": {
        "sbsOrderId": "xxx",
        "packageId": "xxxx"
    }
}

这些字段不应在输出中映射:

[“ shipDate”,“ shipMethod”,“ trackingNumber”,“ quoteId”,“ quote-proof-composition-pageCount”,“ quote-proof-composition-asset”,“ preflightErrorReport”,“ modifiedDt”,“ clientReferenceId “,” schoolIds“,” orderReferenceId“]

我当前的数据结构:

%dw 1.0
%output application/json skipNullOn="everywhere"
---
attribute_name:
            (pl.additionalData mapObject {
(($$) :[$]) when  not ( ($$ contains "shipDate") or ($$ contains "shipMethod") or ($$ contains "trackingNumber") or ($$ contains "quoteId")  or ($$ contains "quote-proof-composition-pageCount") or ($$ contains  "quote-proof-composition-asset") or ($$ contains  "preflightErrorReport") or ($$ contains "modifiedDt") or ($$ contains  "clientReferenceId") or ($$ contains "schoolIds") or ($$ contains "orderReferenceId"))})
    ]]}

它工作正常,但我需要使dw更简单。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

如果您要清理内容,我认为这样做的一个好方法是将不需要的键放入数组中,并检查要映射的对象中的键是否位于其中数组。这样,随着需求的变化,添加/减去字段变得非常容易。

%dw 1.0
%output application/json

%var unwantedKeys = [
  "shipDate",
  "shipMethod",
  "trackingNumber",
  "quoteId",
  "quote-proof-composition-pageCount",
  "quote-proof-composition-asset",
  "preflightErrorReport",
  "modifiedDt",
  "clientReferenceId",
  "schoolIds",
  "orderReferenceId"
]

%function filterUnwantedKeys(value, key)
  {(key): value}
    unless (unwantedKeys contains (key as :string))
    otherwise {}
---
{
  attribute_name: 
    payload.additionalData mapObject filterUnwantedKeys($, $$)
}

答案 1 :(得分:0)

与其删除所有您不想要的内容,不如保留他们想要的内容吗?

%dw 1.0
%output application/json
---
attribute_name: payload.additionalData mapObject {
    sbsOrderId: $.sbsOrderId,
    packageId: $.packageId
}

这产生了所需的输出:

{
  "attribute_name": {
    "sbsOrderId": "TEST-TS-201809-79486",
    "packageId": "AIM_PACKAGE"
  }
}

澄清之后,这是唯一想到的东西,而且没有什么漂亮的东西

%dw 1.0
%output application/json
---
attribute_name: payload.additionalData mapObject (
    $
    -- $["quoteId"]
    -- $["clientReferenceId"]
    -- $["modifiedDt"]
    -- $["orderReferenceId"]
    -- $["shipDate_1"]
    -- $["shipDate_2"]
    -- $["shipDate_3"]
    -- $["shipMethod_1"]
    -- $["shipMethod_2"]
    -- $["shipMethod_3"]
    -- $["trackingNumber_1"]
    -- $["trackingNumber_2"]
    -- $["trackingNumber_3"]
)