i'm forming the JSON keys from JSON values. However sometime one particular key is having special character (* or &) in it. Which is causing error while processing. How do we remove or replace special characters in JSON Keys in Mule 3.
Key with special character: OPTIN*00012AC
Input payload with special character:
{
"request": {
"shippingGroups": [{
"orderAction": "order",
"priceInfo": {
"amount": 10.45
},
"discountInfo": {
"unclaimedCoupon": {},
"orderCoupons": {
"OPTIN*00012AC": {
"promotionDesc": "Promooptintest"
}
},
"orderImplicitDiscountList": [],
"unclaimedCoupon": {},
"claimedCoupon": {
"OPTIN*00012AC": [{
"promotionDesc": "Promooptintest"
}]
}
} }] }}
Expected Output:
{
"request": {
"shippingGroups": [{
"orderAction": "order",
"priceInfo": {
"amount": 10.45
},
"discountInfo": {
"unclaimedCoupon": {},
"orderCoupons": {
"OPTIN00012AC": {
"promotionDesc": "Promooptintest"
}
},
"orderImplicitDiscountList": [],
"unclaimedCoupon": {},
"claimedCoupon": {
"OPTIN00012AC": [{
"promotionDesc": "Promooptintest"
}]
}
} }] }}
@mulesoft #Mule 3
Hi,
As I have replace * with "" with every key may be below Dataweave code will help you.
%dw 2.0
output application/json
import * from dw::core::Objects
fun mappingObject(data) = if((data is Object) == false) data else
data mapObject ((value, key, index) ->
value match {
case value is Object -> (if(key contains('*')) (key replace '*' with "") else key) : mappingObject(value)
case value is Array -> (if(key contains('*')) (key replace '*' with "") else key) : value map (mappingObject($))
else -> (if(key contains('*')) (key replace '*' with "") else key) : value
}
)
---
mappingObject(payload)
Regards,
Shekh