Converting JSON to String in Power Automate A Comprehensive Guide to Parse JSON in Power Apps

Converting JSON to String in Power Automate: A Comprehensive Guide to Parse JSON in Power Apps

Understanding JSON parsing has become essential in modern app development, especially with platforms like Power Automate. In this guide, we will delve deep into how Power Apps allows users to convert JSON to strings and vice versa, providing much-needed flexibility in data handling.

Introduction

With the recent advent of the ParseJSON function, Power Apps now offers a seamless approach to handle JSON data. In this article, we’ll explore various formulas to interpret common JSON structures that you might encounter.

Historically, Power Apps only had limited support for JSON. While converting records and table objects to JSON was feasible via the JSON function, there was no inherent method to do the opposite – converting JSON back to tables or records for app utilization.

Fortunately, Microsoft has made strides in addressing this by unveiling the parseJSON function. We will walk through this functionality in the subsequent sections.

Why Use parseJSON?

Why would developers lean towards parseJSON? Primarily, it becomes vital when dealing with JSON data from databases or web services. Before parseJSON, parsing such data involved convoluted text functions and regex, making it cumbersome.

Parsing a Single JSON Record

Starting off simple, consider a JSON record like the one below:

{ "title": "Mr", "firstName": "Tim", "lastName": "Leung", "age": 40, "email":"[email protected]" }

For demonstration purposes, we’ll use a text input control named ‘inputJSON’ to store the JSON data. The formula to parse such a record would look something like this:

With({parsedObj:ParseJSON(inputJSON.Text)}, Text(parsedObj.firstName) & " " & Text(parsedObj.lastName) )

This formula can be further extended to build an email hyperlink using an HTML text control:

With({parsedObj:ParseJSON(inputJSON.Text)}, $"{Text(parsedObj.firstName)} {Text(parsedObj.lastName)}" )

Parsing Multiple JSON Records

Often, you might have an array of JSON records that need parsing. Here’s a simple formula:

Set(dataRecords, Table(ParseJSON(inputJSON2.Text)) )

For extracting specific field values:

ForAll( Table(ParseJSON(inputJSON2.Text)), { title:Text(Value.title), firstName:Text(Value.firstName), lastName:Text(Value.lastName) } )

Nested Records within a Single JSON Record

Another frequent requirement is parsing a JSON record with nested multiple entries. Here’s a possible approach:

ForAll( Table(ParseJSON(inputJSON3.Text).clients), { title:Text(Value.title), firstName:Text(Value.firstName), lastName:Text(Value.lastName) } )

Parsing Multiple Nested JSON Records

In more complex scenarios where there’s an array of JSON records with multiple nested records, the following formula can be applied:

ForAll( Table(ParseJSON(inputJSON4.Text)), { firstName:Text(Value.firstName), lastName:Text(Value.lastName), phoneDetails: ForAll( Table(Value.phoneDetails), { type:Text(Value.type), number:Text(Value.number) } ) } )

Conclusion

Integrating JSON parsing within Power Apps was a substantial move, addressing one of the most sought-after features. The parseJSON function, despite its initial challenges with untyped objects, is set for future improvements, including the addition of a JSON schema as a potential second argument. This will undoubtedly streamline the JSON parsing process in Power Apps.

If you have further questions or need technical assistance on this topic or any other, please don’t hesitate to contact us. We’re here to help, ensuring you get the most out of your software endeavors.

Enhanced Support Widget