How to create a JSON string array in PowerApps?

Summary

In Power Apps, creating a JSON object is easy. It can be initialized like this.

// Created an object with the types as array of strings
Set (
    anObject,
    {
        service: "Food",
        types: [
            "Bread",
            "Pasta",
            "Soup"
        ]
    }
);

We can use the JSON function to convert the above object to a string. It will give you the JSON string representation to pass to the API as a payload. (Note: I am simplifying the API you may have. It may be complex, but this example will give you an idea.)

Set (
    aJSONStr,
    JSON(anObject)
);

The output of the above call will be the following. Note that the “Value” is added to the string array ‘types’. It will not be a valid payload.

“{\”service\”:\”Food\”,\”types\”:[{\”Value\”:\”Bread\”},{\”Value\”:\”Pasta\”},{\”Value\”:\”Soup\”}]}”

The valid output of the payload string should be:

“{\”service\”:\”Food\”,\”types\”:[\”Bread\”,”Pasta\”,”Soup\”]}”

So, how can we achieve that?

Solution:

So, in Power Apps, that is the expected behavior for the array of strings to add the “Value” property.

The following solution is to replace the highlighted and stricken characters with a blank for every occurrence of the match found.

“{\”service\”:\”Food\”,\”types\”:[ {\”Value\”: \”Bread\” } , {\”Value\”: \”Pasta\” } ,{\”Value\”: \”Soup\” } ]}”

Utilized the Look Ahead Regular Expression technique.


Set (
    aValidJSONStr,
    With (
        {
            InnerWorkStr: JSON(
                anObject,
                JSONFormat.Compact
            )
        },
        With (
            {
                matches: MatchAll(
                    InnerWorkStr,
                    "\{""Value"":(?=.*\})"
                )
            },
            Collect(
                labelTextColl,
                InnerWorkStr
            );
            ForAll(
                matches,
                Collect(
                    labelTextColl,
                    // Find and replace end curly bracket of {\"Value\":\"Bread\"}
                    Replace(
                        // substitute 9 spaces for the string match {\"Value\":
                        Substitute(
                            Last(labelTextColl).Value,
                            ThisRecord.FullMatch,
                            "         "
                        ),
                        Find(
                            "}",
                            Last(labelTextColl).Value,
                            ThisRecord.StartMatch
                        ),
                        1,
                        " "
                    )
                );
            );
            Last(labelTextColl).Value;
            // Very Important as the results returned
        )// End of inner With
    )// End of outer With
);

After executing the above search and replace logic using the logical expression, you will get the valid JSON string with the correct string array as expected for the API payload.

Conclusion

I hope you find this useful for your project as I had to figure this out for my project, and I spent some time understanding the problem and coming up with the solution.

Please also refer to my previous posts related to this post.

How to create a JSON String array in PowerApps?

How to find and replace an array of strings from a large string variable using PowerFx?

Unknown's avatar

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 Apps. Bookmark the permalink.

Leave a comment