Using Liquid Transformations in Logic Apps (Standard)
    • Dark
      Light
    • PDF

    Using Liquid Transformations in Logic Apps (Standard)

    • Dark
      Light
    • PDF

    Article summary

    #ServerlessTips - Azure Logic Apps
    Author: Kent Weare Integration MVP

    In this post, we are going to discuss how we can ‘reshape’ messages using Liquid transformations in Azure Logic Apps (Standard). In my scenario, I want to restrict the number of fields that are returned from a logic app that I am building. My data source is Dataverse and the problem is that the current Dataverse connector, is the legacy connector and I can’t restrict the number of fields returned.

    I will have a multi-record result set returned that has the following structure. I have only included 2 records, but I could have 1 to many records returned. As you can see, we have many attributes/columns being returned, but I don’t want to return all these fields back to my caller.

    {
        "@odata.context": "https://c31b4bcffa5d9790.15.common.logic-westus.azure-apihub.net/apim/commondataservice/4f18ac85e73d4ea785236d86c415c137/$metadata#datasets('https%3A%2F%2Forg7a3fb188.crm.dynamics.com')/tables('cred1_keweare_accounts')/items",
        "value": [
            {
                "@odata.id": "https://org7a3fb188.crm.dynamics.com/api/data/v9.0/cred1_keweare_accounts(1b62ddee-3863-ec11-8f8f-000d3a9d46e5)",
                "@odata.etag": "",
                "ItemInternalId": "1b62ddee-3863-ec11-8f8f-000d3a9d46e5",
                "_owningbusinessunit_value": "34d0a289-186c-ea11-a817-000d3a579c92",
                "_owningbusinessunit_type": "businessunits",
                "modifiedon": "2021-12-22T15:07:57Z",
                "cred1_keweare_accountid": "1b62ddee-3863-ec11-8f8f-000d3a9d46e5",
                "cred1_partnerid": "23456",
                "statecode": 0,
                "_statecode_label": "Active",
                "statuscode": 1,
                "_statuscode_label": "Active",
                "_createdby_value": "6ed8e8e5-4757-40e7-a929-1f3ca161f7fb",
                "_createdby_type": "systemusers",
                "_ownerid_value": "6ed8e8e5-4757-40e7-a929-1f3ca161f7fb",
                "_ownerid_type": "systemusers",
                "cred1_name": "Steve Thomas",
                "cred1_emailaddress": "steve.thomas@contoso.com",
                "_modifiedby_value": "6ed8e8e5-4757-40e7-a929-1f3ca161f7fb",
                "_modifiedby_type": "systemusers",
                "versionnumber": 12631004,
                "createdon": "2021-12-22T15:07:57Z",
                "overriddencreatedon": null,
                "importsequencenumber": null,
                "_modifiedonbehalfby_value": null,
                "utcconversiontimezonecode": null,
                "_createdonbehalfby_value": null,
                "_owningteam_value": null,
                "timezoneruleversionnumber": null
            },
            {
                "@odata.id": "https://org7a3fb188.crm.dynamics.com/api/data/v9.0/cred1_keweare_accounts(45a1c212-3963-ec11-8f8f-000d3a9d46e5)",
                "@odata.etag": "",
                "ItemInternalId": "45a1c212-3963-ec11-8f8f-000d3a9d46e5",
                "_owningbusinessunit_value": "34d0a289-186c-ea11-a817-000d3a579c92",
                "_owningbusinessunit_type": "businessunits",
                "modifiedon": "2021-12-22T15:08:58Z",
                "cred1_keweare_accountid": "45a1c212-3963-ec11-8f8f-000d3a9d46e5",
                "cred1_partnerid": "456789",
                "statecode": 0,
                "_statecode_label": "Active",
                "statuscode": 1,
                "_statuscode_label": "Active",
                "_createdby_value": "6ed8e8e5-4757-40e7-a929-1f3ca161f7fb",
                "_createdby_type": "systemusers",
                "_ownerid_value": "6ed8e8e5-4757-40e7-a929-1f3ca161f7fb",
                "_ownerid_type": "systemusers",
                "cred1_name": "SJ Wiggers",
                "cred1_emailaddress": "SJ@contoso.com",
                "_modifiedby_value": "6ed8e8e5-4757-40e7-a929-1f3ca161f7fb",
                "_modifiedby_type": "systemusers",
                "versionnumber": 12631007,
                "createdon": "2021-12-22T15:08:58Z",
                "overriddencreatedon": null,
                "importsequencenumber": null,
                "_modifiedonbehalfby_value": null,
                "utcconversiontimezonecode": null,
                "_createdonbehalfby_value": null,
                "_owningteam_value": null,
                "timezoneruleversionnumber": null
            }
        ]
    }
    

    Solutions

    In Power Automate, we would typically solve this by creating a loop and then using the Append to Array action. While this will work, it will consume more of our API/action entitlements and will take longer to execute.

    In Azure Logic Apps, we can take advantage of Liquid transformations. If you are using Azure Logic Apps (Consumption), you will require an Integration Account to store your map. However, since we are using Azure Logic Apps (Standard), we can directly upload a liquid transformation into our Azure Logic App service.

    Let’s now create a Liquid transformation. Inside of a text editor, like Visual Studio Code, I can create my liquid transformation based upon the following script.

    {
    "Accounts":[
        {% for account in content.value %}
        {
            "accountId" : "{{account.cred1_keweare_accountid}}",
            "partnerId": "{{account.cred1_partnerid}}",
            "status" : "{{account._statuscode_label}}",
            "accountName" : "{{account.cred1_name}}",
            "emailAddress" :"{{account.cred1_emailaddress}}"
        },
        {% endfor %}
    ]
    }
    

    I will then save this file and then navigate to the Azure Portal where I can upload it into my Azure Logic Apps (Standard) instance.
    1-upload

    Within my logic app, I will add a Transform JSON to JSON action and select my Liquid transformation that I just uploaded.

    Within my Response action, I will return the output from my transformation action.
    2-logicapp(1)

    Testing

    We can now test from Postman and the result will be much cleaner than the payload that I included at the top of this post. This will reduce the noise for any developer who is consuming our service.
    3-output(1)

    Conclusion

    As we saw in this post, we can use Liquid transformations to re-shape message payloads which allows us to build cleaner interfaces.

    If you would like to see this content in video format, please check out the following YouTube video: Using Liquid Transformation in Logic Apps (Standard).


    Was this article helpful?

    ESC

    Eddy AI, facilitating knowledge discovery through conversational intelligence