Blog
Formula - How to parse JSON in Power Apps- 4 examples
Following the introduction of the ParseJSON function, we can more easily parse JSON for use from within Power Apps. This post highlights the formula to parse 4 typical JSON structures that we may encounter.
The great news is that Microsoft has started to address this limitation by introducing the parseJSON function. The documentation for this function is here
https://powerapps.microsoft.com/en-us/blog/power-fx-introducing-parsejson/
https://docs.microsoft.com/en-gb/power-platform/power-fx/working-with-json
What are the use case scenarios of parseJSON?
Why would we want to call parseJSON? A typical scenario is where we want to parse JSON that's stored in a database, or JSON that we retrieve from a web service. 1 - How to parse a single JSON record
{To demonstrate a typical use case, we'll extract the firstName and lastName values and display the concatenated values in a label.
"title": "Mr",
"firstName": "Tim",
"lastName": "Leung",
"age": 40,
"email":"tim@email.com"
}
To make it easier to illustrate the formula in this post, we'll place the input JSON in a text input control called txtJSON. This then avoids the need to escape the double quotes in the JSON that's posted here.
With({obj:ParseJSON(txtJSON.Text)},The screenshot beneath shows the result when we apply this formula to a text label.
Text(obj.firstName) & " " & Text(obj.lastName)
)
With({obj:ParseJSON(txtJSON.Text)},Here's a screenshot of the end result.
$"<a href=""mailto:{Text(obj.email)}"">
{Text(obj.firstName)} {Text(obj.lastName)}
</a>"
)
2 - How to parse an array of multiple JSON records
The second typical scenario is to parse an array of input records and to display the result in a control such as a gallery, data table, or combobox.
Here's an example of how the input data would look.
[
{
"title": "Mr",
"firstName": "Tim",
"lastName": "Leung",
"age": 40,
"email":"tim@email.com"
},
{
"title": "Mrs",
"firstName": "Sally",
"lastName": "Smith",
"age": 30,
"email":"sally@email.com"
},
{
"title": "Mr",
"firstName": "John",
"lastName": "Taylor",
"age": 45,
"email":"john@email.com"
}
]
Set(varRecords,
Table(ParseJSON(txtJSON2.Text))
)
ForAll(
Table(ParseJSON(txtJSON2.Text)),
{
title:Text(Value.title),
firstName:Text(Value.firstName),
lastName:Text(Value.lastName)
}
)
3 - How to parse a single JSON record with nested multiple records
Extending the above example of parsing multiple records, another common structure we can encounter is a single record that contains nested records.
{
"clients":
[
{
"title": "Mr",
"firstName": "Tim",
"lastName": "Leung",
"age": 40,
"email":"tim@email.com"
},
{
"title": "Mrs",
"firstName": "Sally",
"lastName": "Smith",
"age": 30,
"email":"sally@email.com"
}
]
}
ForAll(
Table(ParseJSON(txtJSON3.Text).clients),
{
title:Text(Value.title),
firstName:Text(Value.firstName),
lastName:Text(Value.lastName)
}
)
4 - How to parse multiple JSON records with multiple nested records
|n this last example, we look at how to parse an array of records, each with nested records.
Here's how the input data looks.
[
{
"firstName":"Tim",
"lastName":"Leung",
"phoneNumbers":[
{
"type":"home",
"number":"098322256"
},
{
"type":"work",
"number":"098322085"
}
]
},
{
"firstName":"Sally",
"lastName":"Smith",
"phoneNumbers":[
{
"type":"home",
"number":"098324566"
},
{
"type":"work",
"number":"098389089"
}
]
},
{
"firstName":"John",
"lastName":"Taylor",
"phoneNumbers":[
{
"type":"home",
"number":"093445345"
},
{
"type":"home",
"number":"098354244"
}
]
}
]
ForAll(
Table(ParseJSON(txtJSON4.Text)),
{
firstName:Text(Value.firstName),
lastName:Text(Value.lastName),
phoneNumbers: ForAll(
Table(Value.phoneNumbers),
{
type:Text(Value.type),
number:Text(Value.number)
}
)
}
)
Conclusion
The ability to parse JSON was one of the most requested features and we can now do this with the parseJSON function. This post demonstrated the formula to parse 4 common JSON structures.- Categories:
- formula
- FormuIas - Is it possible to call a user-defined function recursively in Power Apps?
- Formulas - A beginners guide on how to create and call user-defined functions (UDFs)
- Formula - How to add a button that converts degrees Centigrade to Fahrenheit and vice versa
- Formula - How to convert a single delimited string to rows and columns
- Data - How to group data in a gallery and calculate sums
- Formula - How to calculate compound interest
- Utilities - The best way to peform OCR on images of Power Apps Formulas
- Example - How to use a drop down control to convert currencies
- Data - How to get a row by ordinal number
- Formula - What to do when the If statement doesn't work?
- Formula - Boolean And / Or operators - What is the order of precedence?
- Controls - How to set the data source of a Combo Box to a comma separated string
- Numbers - 10 examples of how to round numbers
- Formula - Difference between round, square, and curly brackets
- Top 3 highlights of upcoming enhancements to the Power Apps language (Power FX)
- Email - Sending email attachments with the Office 365 Outlook connector
- Formula - What to try when numbers don't format correctly
- Controls - How to convert HTML to Text
- Formulas - how to return all days between two dates
- Formula - How to create comma separated (CSV) list of items
- Formula - How to use the IF and Switch functions - 3 common examples
- Location - Finding the closest location and and sorting records by distance, based on the current location of the user
- Formulas - How to cope with weekends and public holidays in date calculations