Skip to main content

Notifications

Power Apps - Building Power Apps
Suggested answer

How to create a collection based on a JSON response

Posted on 26 Nov 2024 15:50:00 by 464
Hi All,
 
I have a flow that using Graph API get the users from an given Department.
 
 
 
 
 
 
 
 
 
 
 
The result of this flow is an array called "userarray"
 
{
  "userarray""[{\"@odata.context\":\"https://graph.microsoft.com/v1.0/$metadata#users(id,displayName,department)\",\"value\":[{\"id\":\"80a60ab4-1555-44e8-a1c3-f86982e06bde\",\"displayName\":\"John Taylor\",\"department\":\"IT Specialists\"},{\"id\":\"a3e5b67-95a3-41a4-b8a8-7159805be72c\",\"displayName\":\"Mike Loma\",\"department\":\"IT Specialists\"},{\"id\":\"c076b123-e560-472b-9dfa-ae8f8b3e2625\",\"displayName\":\"Daniel Anderson\",\"department\":\"IT Specialists\"},{\"id\":\"b772b286-8888-481a-b50a-5a2890710c88\",\"displayName\":\"Homero Chase\",\"department\":\"IT Specialists\"}]}]"
}
 
In Power App, I have a button:
 
Set(varUserArray, HolidaysGetUsersDepartment.Run(txtMyDepartment.Value).userarray);
Set(parsedJSONArray, Table(ParseJSON(varUserArray)));
 
ClearCollect(
    colUsersDepartment,
    ForAll(
        parsedJSONArray,
        {
            DisplayName: Text(Value.displayName),
            Department: Text(Value.department)
        }
    )
)
 
But it is not working, the collection "colUsersDepartment" is empty.
 
Thanks for any help!!
 
  • EmilioRoqueta69 Profile Picture
    EmilioRoqueta69 464 on 26 Nov 2024 at 16:58:29
    How to create a collection based on a JSON response
    Hi @Mitanshu,
     
    Thanks for your help!
    I get this error using your code:
    Variable parsedJSON value is blank. 
     
     
     
     
     
    This is the full value of variable "varUserArray" (if it helps!)
     

    [{"@odata.context":"https://graph.microsoft.com/v7.0/$metadata#users(id,displayName,department)","value":[{"id":"40a60ab4-7492-44e4-a7c9-f46942e06bde","displayName":"John Castroman","department":"IT Specialists"},{"id":"a9e7b72e-97a9-47a4-b4a4-7779407be72c","displayName":"Adrian Lloma","department":"IT Specialists"},{"id":"c076b729-e294-472b-9dfa-ae4f4b9e2627","displayName":"Robert Lopez","department":"IT Specialists"},{"id":"b772b246-7407-447a-b70a-ceab624d04e0","displayName":"Victor Rosario","department":"IT Specialists"},{"id":"f60ceb7e-c097-4729-9f9b-4979d7fd7abd","displayName":"Emilio Anderson","department":"IT Specialists"},{"id":"49ce4747-7977-4fce-a470-9b44099cf00c","displayName":"Laura Taylor","department":"IT Specialists"},{"id":"926a744f-69ac-4b44-97a2-7a2490770c44","displayName":"Homero Simpson","department":"IT Specialists"}]}]
  • Suggested answer
    Pstork1 Profile Picture
    Pstork1 63,797 on 26 Nov 2024 at 16:21:58
    How to create a collection based on a JSON response
    Mitanshu's code will work, but the easier way to do it is use the HTTP Response action to return the array as typed data directly to the collection. The one downside is that it requires a Premium license. If you have Premium I recommend using the HTTP Response. If not the other code will also work.
  • Suggested answer
    Mitanshu Profile Picture
    Mitanshu 1,574 on 26 Nov 2024 at 16:03:36
    How to create a collection based on a JSON response
    Can you try the code below:
     
    Set(varUserArray, HolidaysGetUsersDepartment.Run(txtMyDepartment.Value).userarray);
    
    Set(parsedJSON, JSON(varUserArray));
    
    Set(parsedJSONArray, Table(ForAll(Value(parsedJSON), Value)));
    
    ClearCollect(
        colUsersDepartment,
        ForAll(
            parsedJSONArray,
            {
                DisplayName: Text(Value.displayName),
                Department: Text(Value.department)
            }
        )
    );
    
     
    Notes:
    1. JSON(varUserArray): Converts the stringified JSON into a proper JSON object.
    2. Value(parsedJSON): Retrieves the value array from the JSON object (which contains the user details).
    3. Table(ForAll(Value(parsedJSON), Value)): Extracts each item in the array for further processing.
    4. ClearCollect for colUsersDepartment: Builds a collection from the parsed JSON array.

     

    Finally: Use a Label to display intermediate variables like parsedJSON to verify their structure during debugging.
     
     
    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If this post or my previous reply was useful in other ways, please consider giving it Thumbs Up.
     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

November 2024 Newsletter…

November 2024 Community Newsletter…

Community Update Oct 28…

Power Platform Community Update…

Tuesday Tip #7 Community Profile Tips…

Welcome to a brand new series, Tuesday Tips…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 143,129

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 63,797

Leaderboard