Liquid Map Arithmetic Operations

Viewed 39

Hello wise people of StackOverflow, I have an exchange rates file that I am mapping from XML to JSON in an Azure logic app using Transform XML To JSON. I have two rates (Ask and Bid) within the XML. A node in the XML file looks like so:

<quote>
      <ask>110.0668027</ask>
      <bid>108.1461645</bid>
      <currency>AFA</currency>
      <date>2022-07-27T23:59:59+0000</date>
      <midpoint>109.1064836</midpoint>
</quote>

I then need to map to JSON but I wish for the JSON output to only show a single rate which must be calculated as (Ask+Bid)/2 to give me the average.

I have created a liquid map like so:

{
    
"EffectiveDate": "2022-06-06",
"BaseCurrency": "GBP",  
"ExchangeRates": 
    [
        {% for data in content.response.quotes %}
                {
                        "ExchangeRate": "({{data.quote.ask}} + {{data.quote.bid}})/2",
                        "TargetCurrency": "{{data.quote.currency}}"
                        
                },
                
                
        {%endfor%}
    ]
}

But when I run the logic app the JSON output looks like so:

{
  "ExchangeRate": "(110.0668027 + 108.1461645)/2",
  "TargetCurrency": "AFA"
},

Which is obviously not what I want to see! What is wrong with the syntax in my Liquid map?

1 Answers

In Liquid you have to use math filters to manipulate integers.

So I would do something like that:

First calculate your value to divide.

{% assign value_2 = value_2 | divided_by:2 %} 

Then output your values by adding them:

{{ value_1 | plus: value_2}} 

More info about math filters here: Documentation about math filters

Related