Skip to main content

Notifications

Power Apps - Building Power Apps
Suggested answer

How to create a collection based on a JSON response

Posted on by 470
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 470 on at
    How to create a collection based on a JSON response
    I fixed the flow and now the json response is working.
  • EmilioRoqueta69 Profile Picture
    EmilioRoqueta69 470 on at
    How to create a collection based on a JSON response
    Hi @Mitanshu,
     
    Thanks for the follow-up!
    But I still get an error when paste your last formula.
     
    Did you try to transform this Json example into the collection?
     
    [{"@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
    Mitanshu Profile Picture
    Mitanshu 1,584 on at
    How to create a collection based on a JSON response
    Can you try below (slight variation is previous code)

    Can you add some labels at each step for debugging.
     
    Set(varUserArray, HolidaysGetUsersDepartment.Run(txtMyDepartment.Value).userarray);
    
    // Parse JSON string into a usable object
    Set(parsedJSON, JSON(Value(varUserArray)));
    
    // Extract values from the JSON array into a collection
    ClearCollect(
        colUsersDepartment,
        ForAll(
            Table(parsedJSON.value), // Access the "value" array within the JSON object
            {
                DisplayName: Text(ThisRecord.displayName),
                Department: Text(ThisRecord.department)
            }
        )
    );
     
  • EmilioRoqueta69 Profile Picture
    EmilioRoqueta69 470 on at
    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 64,014 on at
    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,584 on at
    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,487

#2
RandyHayes Profile Picture

RandyHayes 76,308

#3
Pstork1 Profile Picture

Pstork1 64,014

Leaderboard