簡體   English   中英

使用 Dataweave 2.0 轉換 JSON Object

[英]Transforming a JSON Object using Dataweave 2.0

我收到來自 API GET 請求的 JSON 響應到我的 Mule 流中,看起來有點像下面的 JSON。 我正在嘗試 map 以實現 output 列表,其中每個客戶看起來有點像下面。 問題是位於自定義屬性下的公司卡號,而且我一直在底部顯示錯誤。 我試圖使用有效載荷 map (value,key) function 這給了我這個錯誤。 當我使用 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": ???
        }
}

對您要查找的內容進行了一些猜測。

%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