Search code examples
muledataweavemulesoft

How to update Currency Symbols dynamically for each fields based on provided Country


With this static payload, how will I able to change those currency symbols based on the country provided in the request

[
    {
        "orderId": "11111101",
        "details": {
            "TotalPrice": {
                "NetPrice__c": 11.11,
                "NetPriceFormatted__c": "11,11 $"
            },
            "TotalCartValue__c": 11.11,
            "TotalCartValueFormatted__c": "11,11 $",
            "TotalTaxPrice__c": 11.11,
            "TotalTaxPriceFormatted__c": "11,11 $",
            "TotalDiscountPrice__c": 11.11,
            "TotalDiscountPriceFormatted__c": "11,11 $",
            "products": [
                {
                    "SKUNumber__c": "111111-01",
                    "Quantity": "1",
                    "UnitPrice": 11.11,
                    "UnitPriceFormatted__c": "11,11 $",
                    "TotalPrice": 11.11,
                    "TotalPriceFormatted__c": "11,11 $",
                    "Discount__c": 11.11,
                    "DiscountFormatted__c": "11,11 $",
                    "ProductName__c": "Apple",
                    "MachineFriendlyName__c": "iPhone",
                    "DiscountValue__c": 11.11,
                    "bundles": []
                }
            ],
             "shippingModes": [
                "ClickAndCollect",
                "ScheduledDelivery",
                "StandardDelivery"
            ],
            "TotalItems__c": 1,
            "PromoCode__c": null,
            "PromoStatus__c": null
        }
    },
    {
        "orderId": "22222202",
        "details": {
            "TotalPrice": {
                "NetPrice__c": 22.22,
                "NetPriceFormatted__c": "22,22 $"
            },
            "TotalCartValue__c": 22.22,
            "TotalCartValueFormatted__c": "22,22 $",
            "TotalTaxPrice__c": 22.22,
            "TotalTaxPriceFormatted__c": "22,22 $",
            "TotalDiscountPrice__c": 22.22,
            "TotalDiscountPriceFormatted__c": "22,22 $",
            "products": [
                {
                    "SKUNumber__c": "222222-02",
                    "Quantity": "1",
                    "UnitPrice": 22.22,
                    "UnitPriceFormatted__c": "22,22 $",
                    "TotalPrice": 22.22,
                    "TotalPriceFormatted__c": "22,22 $",
                    "Discount__c": 22.22,
                    "DiscountFormatted__c": "22,22 $",
                    "ProductName__c": "Samsung",
                    "MachineFriendlyName__c": "Galaxy",
                    "DiscountValue__c": 22.22,
                    "bundles": []
                }
            ],
             "shippingModes": [
                "ClickAndCollect",
                "ScheduledDelivery",
                "StandardDelivery"
            ],
            "TotalItems__c": 1,
            "PromoCode__c": null,
            "PromoStatus__c": null
        }
    }
]

For example, the country in the request is Italy and filtering with orderId of 11111101

the result will be: (same structure of the static payload, only difference is the currency symbols will change)

[
    {
        "orderId": "11111101",
        "details": {
            "TotalPrice": {
                "NetPrice__c": 11.11,
                "NetPriceFormatted__c": "11,11 €"
            },
            "TotalCartValue__c": 11.11,
            "TotalCartValueFormatted__c": "11,11 €",
            "TotalTaxPrice__c": 11.11,
            "TotalTaxPriceFormatted__c": "11,11 €",
            "TotalDiscountPrice__c": 11.11,
            "TotalDiscountPriceFormatted__c": "11,11 €",
            "products": [
                {
                    "SKUNumber__c": "111111-01",
                    "Quantity": "1",
                    "UnitPrice": 11.11,
                    "UnitPriceFormatted__c": "11,11 €",
                    "TotalPrice": 11.11,
                    "TotalPriceFormatted__c": "11,11 €",
                    "Discount__c": 11.11,
                    "DiscountFormatted__c": "11,11 €",
                    "ProductName__c": "Apple",
                    "MachineFriendlyName__c": "iPhone",
                    "DiscountValue__c": 11.11,
                    "bundles": []
                }
            ],
             "shippingModes": [
                "ClickAndCollect",
                "ScheduledDelivery",
                "StandardDelivery"
            ],
            "TotalItems__c": 1,
            "PromoCode__c": null,
            "PromoStatus__c": null
        }
    }
]

Solution

  • You can see this recursive solution to update each field based on values present on countryNames variable and replace it with your required symbol. You can refer more about replace here

    Dataweave

    %dw 2.0
    output application/json
    var countryNames = {
        "England": "£",
        "Philliphines": "₱"
    }
    fun replaceDollarWithCountryValue(data: Any, country: String): Any =
        data match {
            case is Array -> data map replaceDollarWithCountryValue($, country)
            case is Object -> $ mapObject ((value, key, index) ->
                (key): replaceDollarWithCountryValue(value, country))
            
            else -> if (($ is String)and($ contains "\$"))
                        $ replace "\$" with countryNames[country] 
                    else $
        }
    ---
    replaceDollarWithCountryValue(payload, "England")
    

    Output

    [
      {
        "orderId": "11111101",
        "details": {
          "TotalPrice": {
            "NetPrice__c": 11.11,
            "NetPriceFormatted__c": "11,11 £"
          },
          "TotalCartValue__c": 11.11,
          "TotalCartValueFormatted__c": "11,11 £",
          "TotalTaxPrice__c": 11.11,
          "TotalTaxPriceFormatted__c": "11,11 £",
          "TotalDiscountPrice__c": 11.11,
          "TotalDiscountPriceFormatted__c": "11,11 £",
          "products": [
            {
              "SKUNumber__c": "111111-01",
              "Quantity": "1",
              "UnitPrice": 11.11,
              "UnitPriceFormatted__c": "11,11 £",
              "TotalPrice": 11.11,
              "TotalPriceFormatted__c": "11,11 £",
              "Discount__c": 11.11,
              "DiscountFormatted__c": "11,11 £",
              "ProductName__c": "Apple",
              "MachineFriendlyName__c": "iPhone",
              "DiscountValue__c": 11.11,
              "bundles": [
                
              ]
            }
          ],
          "shippingModes": [
            "ClickAndCollect",
            "ScheduledDelivery",
            "StandardDelivery"
          ],
          "TotalItems__c": 1,
          "PromoCode__c": null,
          "PromoStatus__c": null
        }
      },
      {
        "orderId": "22222202",
        "details": {
          "TotalPrice": {
            "NetPrice__c": 22.22,
            "NetPriceFormatted__c": "22,22 £"
          },
          "TotalCartValue__c": 22.22,
          "TotalCartValueFormatted__c": "22,22 £",
          "TotalTaxPrice__c": 22.22,
          "TotalTaxPriceFormatted__c": "22,22 £",
          "TotalDiscountPrice__c": 22.22,
          "TotalDiscountPriceFormatted__c": "22,22 £",
          "products": [
            {
              "SKUNumber__c": "222222-02",
              "Quantity": "1",
              "UnitPrice": 22.22,
              "UnitPriceFormatted__c": "22,22 £",
              "TotalPrice": 22.22,
              "TotalPriceFormatted__c": "22,22 £",
              "Discount__c": 22.22,
              "DiscountFormatted__c": "22,22 £",
              "ProductName__c": "Samsung",
              "MachineFriendlyName__c": "Galaxy",
              "DiscountValue__c": 22.22,
              "bundles": [
                
              ]
            }
          ],
          "shippingModes": [
            "ClickAndCollect",
            "ScheduledDelivery",
            "StandardDelivery"
          ],
          "TotalItems__c": 1,
          "PromoCode__c": null,
          "PromoStatus__c": null
        }
      }
    ]