简体   繁体   中英

Mule Dataweave: using dynamic key while transforming json to xml

I am trying to use Mule dataweave transformer to transform a JSON to an XML. One of the key name of the JSON payload is dynamic and it depends on the input you provide to the JSON payload. Please look at the line #40 where I am using

value: payload.quotes.INR.midpoint

to parse the data. INR is basically the target currency of the payload and the target currency value is line #39 :

target_currency: payload.meta.effective_params.quote_currencies[0]

So while assigning the value I want to use something like this:

value: payload.quotes.($(payload.meta.effective_params.quote_currencies[0])).midpoint

Is this possible at all with Mulesoft dataweave transformer? Or, if not, then can you please suggest any neat solution?

Yes it is, use square brackets [] to lookup the map by key:

exchange: {
        source_currency: payload.base_currency,
        target_currency: payload.meta.effective_params.quote_currencies[0],
        value: payload.quotes[payload.meta.effective_params.quote_currencies[0]].midpoint
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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