Using Liquid Template in Logic App

I now have the luxury to use Azure Integration Account. When i came to a task to convert JSON to JSON, I immediately thought i should try Liquid Template. It turned out pretty easy to draft it until i loaded it to integration account and tested in Logic App. It just keeps failing with error:

{
  "Code": "IncorrectLiquidTransformOutputType",
  "Message": "An error occurred while converting the transformed value to JSON. The transformed value is not a valid JSON. 'After parsing a value an unexpected character was encountered: {. Path 'Lines.Line[0]', line 25, position 6.'"

The error message doesn’t help much and it seems there’s no output once the output is not a valid JSON. So testing liquid template in Logic App is not ideal.

Luckily i found this post Test your Liquid transformations without deployment and used that tool to do liquid template testing. It will give you output even though the output is an invalid JSON. And then you online json validator to pinpoint the error.

During testing i found there’re 2 caveats i faced and causing me issues:

  1. Redundant comma at the end of Json array item.
    You see below json array, i had to add an condition for the last item in the array, not to add a comma.
  2. You need to deal with null value, otherwise you would have an error when calling the map.
{
  "AccountName": "{{content.BID_STD_PRICE_.WDBHDR.AccountName}}",
  "BFBranch": "{{content.BID_STD_PRICE_.WDBHDR.Branch}}",
  "BFBranchName": "{{content.BID_STD_PRICE_.WDBHDR.BranchName}}",
  "SiteID": null,
  "Lines": {
    "Line": [
	{%- for Line in content.BID_STD_PRICE_.WDBDTL -%} 
       {%- if forloop.last == true -%}
      {
        "ProductCode": {%- if Line.ProductCode != null -%}"{{Line.ProductCode}}",{%- else -%}null,{%- endif -%}
        "SupplierProductCode": null,       
        "Brand": {%- if Line.Brand != null -%}"{{Line.Brand}}",{%- else -%}null,{%- endif -%}       
        "TaxRate": {%- if Line.TaxRate != null -%}"{{Line.TaxRate}}"{%- else -%}null,{%- endif -%}
      }
    {%- else -%}
      {
"ProductCode": {%- if Line.ProductCode != null -%}"{{Line.ProductCode}}",{%- else -%}null,{%- endif -%}
        "SupplierProductCode": null,       
        "Brand": {%- if Line.Brand != null -%}"{{Line.Brand}}",{%- else -%}null,{%- endif -%}       
        "TaxRate": {%- if Line.TaxRate != null -%}"{{Line.TaxRate}}"{%- else -%}null,{%- endif -%}
},
   {%- endif -%}
{%- endfor -%}
]
  }
}

To automate the liquid template to integration account

Add an Azure Powershell task to devops release pipeline. I’m using New-AzIntegrationAccountMap and Set-AzIntegrationAccountMap cmdlet which in Az.LogicApp module. Az Powershell 7.5.0. So specify the version number in the Azure Powershell task.

Here’s the script:

Get-ChildItem "$(System.DefaultWorkingDirectory)/$(edi.templatesPath)/edi-batch-biztalk-maps-ia/" -Filter *.liquid | 
Foreach-Object {
    $mapName=[io.path]::GetFileNameWithoutExtension($_)
    $mapFileName=$_
    Write-Host "Deploying map:$mapName from file:$mapFileName"

    try 
	{
	    #throws an error if the map does not exist
		Set-AzIntegrationAccountMap -ResourceGroupName "edi-$(edi.env)-rg" -Name "edi-$(edi.env)-ia" -MapName $mapName -MapType Liquid -mapFilePath "$(System.DefaultWorkingDirectory)/$(edi.templatesPath)/edi-batch-biztalk-maps-ia/$mapFileName" -Force
	}
	catch 
	{
		New-AzIntegrationAccountMap -ResourceGroupName "edi-$(edi.env)-rg" -Name "edi-$(edi.env)-ia" -MapName $mapName -MapType Liquid -mapFilePath "$(System.DefaultWorkingDirectory)/$(edi.templatesPath)/edi-batch-biztalk-maps-ia/$mapFileName"
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s