简体   繁体   English

使用 Dataweave 2.0 转换 JSON Object

[英]Transforming a JSON Object using Dataweave 2.0

I am getting a JSON response from an API GET request into my Mule flow which looks somewhat like the below JSON.我收到来自 API GET 请求的 JSON 响应到我的 Mule 流中,看起来有点像下面的 JSON。 I am trying to map this to achieve an output list where each customer looks somewhat like below.我正在尝试 map 以实现 output 列表,其中每个客户看起来有点像下面。 problem is the company card number which sits under custom attributes and also I keep getting the error show at the bottom.问题是位于自定义属性下的公司卡号,而且我一直在底部显示错误。 I have tried to use the payload map (value,key) function which is giving me that error.我试图使用有效载荷 map (value,key) function 这给了我这个错误。 When I use pluck I get all items listed under id, firstname etc but I want them individually soa repeated list.当我使用 pluck 时,我会在 id、firstname 等下列出所有项目,但我希望它们单独出现重复列表。
Thank you!谢谢!

{
    "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": ???
        }
}

Made a few guesses as to what you're looking for.对您要查找的内容进行了一些猜测。

%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($))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM