How to use JOSN data in Canvas APP received from a web API response
To use the JSON response of a Custom Connector in a Canvas App in Power Apps, you can access, parse, and display the data directly within the app. Here’s a step-by-step guide to achieve this.
You can watch the video or scroll down to read.
Step 1: Add the Custom Connector to Power Apps
- Go to Power Apps (https://make.powerapps.com/).
- Select Apps and create or edit a Canvas App.
- Click Data (left panel) and click Add data.
- Search for and add your Custom Connector.
💡 Note: If you don’t see the connector, make sure it’s shared with you and available in your environment.
Step 2: Call the Custom Connector in Power Apps
- Add a Button to the Canvas app to trigger the API call.
- Add the following formula in the OnSelect property of the button:powerappsCopy code
ClearCollect(MyData, YourCustomConnector.YourActionName())
Explanation:MyData
: A local collection that will store the JSON response.YourCustomConnector
: The name of the custom connector.YourActionName
: The action name inside the connector that returns the JSON response.
- Click the button to trigger the API call and store the response in
MyData
.
Step 3: Parse the JSON Data
Once the JSON response is in MyData, you can access its fields using dot notation.
Example JSON Response:
jsonCopy code{
"status": "success",
"data": [
{
"userId": "12345",
"userName": "JohnDoe",
"email": "johndoe@example.com",
"roles": ["Admin", "Editor"]
},
{
"userId": "67890",
"userName": "JaneDoe",
"email": "janedoe@example.com",
"roles": ["User", "Editor"]
}
]
}
To Parse this Data:
- Access individual properties from
MyData
using Power Apps dot notation.
Example:powerappsCopy codeFirst(MyData).status
- If
data
is an array, you can loop through it using a Gallery.- Insert a Gallery control.
- Set the Items property of the gallery to:powerappsCopy code
MyData.data
Step 4: Display Data in Power Apps
- Bind a Gallery to the
data
array.- Set Items property of the gallery to:powerappsCopy code
MyData.data
- Set Items property of the gallery to:powerappsCopy code
- Display Fields in Labels inside the gallery:
- Add a Label control inside the gallery.
- Set the Text property to:powerappsCopy code
ThisItem.userName
email
,roles
, etc.
Complete Walkthrough Example
Scenario
You have a Custom Connector called UserAPI
with an action GetUsers
, which returns the following JSON:
jsonCopy code{
"status": "success",
"data": [
{ "userId": "12345", "userName": "JohnDoe", "email": "johndoe@example.com" },
{ "userId": "67890", "userName": "JaneDoe", "email": "janedoe@example.com" }
]
}
Steps
- Add a Button to call the Custom Connector.
OnSelect property of the button:powerappsCopy codeClearCollect(UserCollection, UserAPI.GetUsers())
- Add a Gallery to display the response.
Items property of the gallery:powerappsCopy codeUserCollection.data
- Inside the Gallery, add Label controls.
Set Text property for each label:powerappsCopy codeThisItem.userName
- Preview the app, click the button, and you’ll see a list of usernames displayed.
Access Specific Fields from JSON
If you want to access specific parts of the JSON (like nested arrays), you can use Power Apps functions like:
- First(): Get the first record from the collection.
- LookUp(): Look up a specific record.
- ThisItem: Reference current item in a gallery or loop.
- Text(): Extract text from a value.
Example Usage
- To get the
email
of the first user:powerappsCopy codeFirst(UserCollection.data).email
- To show a role from the
roles
array, you can use:powerappsCopy codeFirst(UserCollection.data).roles[1] // Shows the first role
Best Practices
- Clear existing data: Use
ClearCollect()
to avoid appending old data. - Error handling: Add a label to show errors from the connector, like:powerappsCopy code
If(IsEmpty(UserCollection), "No data found", "Data loaded successfully")
- Use Global Variables: If you want to store the response globally, you can use:powerappsCopy code
Set(UserData, UserAPI.GetUsers())
Summary
- Call Custom Connector: Use
ClearCollect(MyData, YourCustomConnector.YourActionName())
. - Parse JSON: Access the collection with
MyData.data
and bind it to a Gallery. - Access JSON Fields: Use
ThisItem.userName
,ThisItem.email
, etc.
With this approach, you can work with any Custom Connector response in a Power Apps Canvas App.
hope it helps.