How to create a JSON String array in PowerApps?

Summary

I needed to pass from a PowerApps the following JSON payload to a Power Automate Run parameter. I have just added a few properties of the large payload to remove complexity. This way I can tell you the problem with the PowerApps JSON string array.

// The following array is required as an output
Set (
    varRegistryFiltersArray,
    [
        {
            conditions: ["Diabetes"],  
            facilityLocations: [
                {
                    state: "CA",
                    country: "United States"
                }
            ]
        }
    ]
);
// But the JSON function created different output.
Set(
    varRegistryFiltersArrayString,
    JSON(
        varRegistryFiltersArray,
        JSONFormat.Compact
    )
);

When I ran the JSON function on the above variable ‘varRegistryFiltersArray’, I got the following output. As you can see I did not want the “Value”: “Diabetes” and did not want conditions as an array of objects.

[
    {

        // This was not expected output. PowerApps treats string array like this.
        "conditions": [
            {
                "Value": "Diabetes"
            }
        ],
        "facilityLocations": [
            {
                "country": "United States",
                "state": "CA"
            }
        ]
    }
]

This is a default behavior of the Power Apps String array (it treats as a Table) to produce the record for each string in the array as a record with a Value column. I searched but have not found anything simpler so I created this technique.

Wherever I have the string array I created the following tokens. The tokens are surrounded by the inner string array and later the tokens are substituted/ removed for the final JSON string using the Substitute function.

Set (
    varRegistryFiltersArray,
    [
        {
            conditions: [{replace: "Diabetes,replace"}],
            facilityLocations: [
                {
                    state: "CA",
                    country: "United States"
                }
            ]
        }
    ]
);
Set(
    varRegistryFiltersArrayString,
    JSON(
        varRegistryFiltersArray,
        JSONFormat.Compact
    )
);
//
// substitute "{replace:"  with ""   AND 
// substiture "replace\"}" with """"
// make a note of $ sign before string.
Set(
    varRegistryFiltersArrayString,
    Substitute(
        Substitute(
            varRegistryFiltersArrayString,
            $"{{""replace"":",
            ""
        ),
        $",replace""}}",
        $""""
    )
);

Conclusion

This technique worked for my large JSON payload. I hope it works for you this simple trick.

About Pankaj

I am a Developer and my linked profile is https://www.linkedin.com/in/pankajsurti/
This entry was posted in Power Apps, Power Automate. Bookmark the permalink.

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 )

Facebook photo

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

Connecting to %s