I am working on a Power Apps application where I need to handle data returned from an API dynamically. The API response contains nested JSON objects, arrays, and sometimes objects within arrays. The challenge is that the structure of the JSON can change over time, making it impractical to hardcode field names or manually map properties.
Here are two examples of the API response structures I receive:
Example 1:
{
"content": [
{
"name": "Marywood University",
"alpha_two_code": "US",
"domains": ["marywood.edu"],
"country": "United States",
"web_pages": ["https://www.google.com/"],
"state-province": null
},
{
"name": "Lindenwood University",
"alpha_two_code": "US",
"domains": ["lindenwood.edu"],
"country": "United States",
"web_pages": ["https://www.google.com/"],
"state-province": null
}
]
}
Example 2:
{
"results": [
{
"gender": "female",
"name": {
"title": "Ms",
"first": "Jessica",
"last": "Perry"
},
"location": {
"street": {
"number": 8932,
"name": "Manor Road"
},
"city": "Kilcoole",
"state": "Meath",
"country": "Ireland"
},
"email": "[email protected]"
}
]
}
I need to dynamically process this data in Power Apps without hardcoding field names, while still accommodating changes in the API’s response format.
What did you try and what were you expecting?
-
Tried Flattening in Power Apps:
- I used functions like
ParseJSON
,Table
, andForAll
to dynamically extract the properties of the JSON. However, Power Apps lacks native support for iterating over object properties or handling arrays within nested JSON dynamically.
- I used functions like
-
Tried Key-Value Pair Mapping:
- I attempted to extract key-value pairs from the JSON using nested
ForAll
loops, but encountered errors likeName isn't valid
orText function has invalid arguments
when processing dynamic fields likePropertyName
andPropertyValue
.
- I attempted to extract key-value pairs from the JSON using nested
Expected Outcome:
I want to create a solution where Power Apps can dynamically process JSON data (including nested objects and arrays), regardless of the structure, and display it in a gallery or collection without requiring manual adjustments each time the API changes.
2
Answers
APIs are handled by Power Automate;
If you’re using the standard HTTP Response block you can’t have dynamic API responses with this block as it requires a response schema. Best bet would be to have multiple blocks designed for each response format and return the correct one to PowerApps.
However if you’re handling the API response inside Power Automate and manipulating it, then returning the contents to PowerApps as in the below image. You have more flexibility as to what is being returned to PowerApps, if you’re calling an endpoint with a HTTP block then you’ll still need to have a schema which handles the response.
JSON contents being returned to PowerApp
Inside PowerApps you can then handle JSON and untyped objects.
Then you can use the dot notation to extract fields.
Example with an array
Parsing the above response from a Power Automate flow.
To be able to display an arbitrary response in a gallery, you need to know something about its shape. If the possible responses follow the pattern you showed above (an object, with a single property, whose value is an array of records), then you can use something like the expression below to convert from that JSON into a table with nested data:
Notice the
IfError
usage above. If the value of the property is a primitive type (number, boolean, text), then we can convert it directly to text. If not (i.e., it’s a nested record or array), then I’m using the JSON function to stringify it so that we have a consistent type to use later. You could also use some other logic to convert those values to text.If you use that expression in a gallery, then inside of it you can access the members of the
record
property, for example, to display in a label:Some examples from your input data:
Example 1:
Example 2: