【问题标题】:Transforming a JSON Object using Dataweave 2.0使用 Dataweave 2.0 转换 JSON 对象
【发布时间】:2021-02-16 17:49:54
【问题描述】:

我收到来自 API GET 请求的 JSON 响应到我的 Mule 流中,它看起来有点像下面的 JSON。我试图映射它以实现每个客户看起来有点像下面的输出列表。问题是位于自定义属性下的公司卡号,而且我一直在底部显示错误。我试图使用给我这个错误的有效载荷映射(值,键)函数。当我使用 pluck 时,我会在 id、firstname 等下列出所有项目,但我希望它们单独出现重复列表。
谢谢!

{
    "items": [
        {
            "id": 17837,
            "group_id": 2,
            "email": "catherinebrugge@tprg.com",
            "firstname": "Catherine5",
            "lastname": "Brugge",
            "custom_attributes": [
                {
                    "attribute_code": "customer_id",
                    "value": "29a8-303b-01c7fe28"
                }
            ]
        },
        {
            "id": 17839,
            "group_id": 2,
            "email": "catherineb@123.com",
            "firstname": "Catherine",
            "lastname": "Brugge",
            "website_id": 2,
            "addresses": [
                {
                    "id": 33773,
                    "customer_id": 17839,
                    "region": {
                        "region_code": null,
                    },
                    "region_id": 0,
                    "street": [
                        "123 Kings Road"
                    ],
                    "default_billing": true
                }
            ],
            "disable_auto_group_change": 0,
            "custom_attributes": [
                {
                    "attribute_code": "company_card_number",
                    "value": "100000000"
                },
                {
                    "attribute_code": "ustomer_id",
                    "value": "29a8-c3443e-014062"
                }
            ]
        },
        {
            "id": 18357,
            "group_id": 2,
            "email": "catherinebrugge@543.com",
            "firstname": "Catherine",
            "lastname": "Brugge",
            "custom_attributes": [
                {
                    "attribute_code": "company_card_number",
                    "value": "888243"
                },
                {
                    "attribute_code": "customer_id",
                    "value": "2b02-88583c-021559"
                }
            ]
        }
    ],
    "search_criteria": {
        "filter_groups": [
            {
                "filters": [
                    {
                        "field": "group_id",
                        "value": "2",
                        "condition_type": "equal"
                    }
                ]
            }
        ]
    },
    "total_count": 3
}
You called the function 'map' with these arguments: 

1: Object ({items: [{id: ...)
 2: Function ((value:Any, key:Any) -> ???)

{
    "customers":{
        "id": payload.items.id,
        "group_id": payload.items.group_id,
        "firstname":payload.firstname,
        "lastname": payload.lastname,
        "email": payload.email,
        "company_card_number": ???
        }
}

【问题讨论】:

  • 你好凯瑟琳,请告诉我们这里想要的输出是什么?所以我们可以帮助您改变这一点
  • 请同时添加完整的当前 DataWeave 脚本和完整的错误消息。

标签: json mule dataweave


【解决方案1】:

对您要查找的内容进行了一些猜测。

%dw 2.0
output application/json  skipNullOn="everywhere"

fun getCardNumber(customer) = customer.custom_attributes[?($.attribute_code == 'company_card_number')][0].value

---
customers:payload.items map ($ - "custom_attributes" ++ "company_card_number": getCardNumber($))

【讨论】:

  • 非常感谢 Dale,这很好用!我的下一个问题是我想过滤 company_card_number 上的客户,但即使号码存在,我总是得到一个空对象。我已经尝试过带引号和不带引号但没有返回任何内容...谢谢! %dw 2.0 output application/json --- payload filter (payload.customers.company_card_number == "100000000")
  • 当我尝试过滤任何电子邮件地址时,我遇到了同样的问题。它只是返回 [ ]
  • @CatherineBrugge 不太清楚你在说什么。您能否发布另一个包含输入、预期输出和您的数据编织的问题,我会看一下吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多