cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
in2217
Level: Powered On

Iterating over JSON elements, pulling all first level keys?

Hi,

 

here is looking for a way to iterate over JSON elements that are themselves JSONs.

JSON looks like this:

{
"key1": {
    "value": 5,
    "old_value": 4,
    "added": null,
    "removed": null,
    "type": "Integer",
    "name": "name1"
  },
  "key2": {
    "value": "value2",
    "old_value": null,
    "added": null,
    "removed": null,
    "type": "String",
    "name": "name2"
  }
}
 
Need to pull all first-level keys (key1, key2 etc) into array.
 
The ultimate goal is to check whether any of the first level keys is a member of the predefined array, and if yes, pull it aside into array variable for processing.
 
E.g. an array is [key0, key1, key2, key3]. First task is to check JSON keys vs this list.
So key1 will return TRUE and key19 will return FALSE.
 
Thank you.
1 ACCEPTED SOLUTION

Accepted Solutions
Community Support Team
Community Support Team

Re: Iterating over JSON elements, pulling all first level keys?

Hi @in2217 ,

 

Please try the following method.

 

  • Initialize three variables, the first one is used to check if the key exists, and how many keys will be configured according to your own actual situation.
  • The second variable is used to store JSON Data that needs to be checked, and the third variable is used to store the first level keys.
  • Traverse checkList, using the current traversed element as the property name of JSON to get the property value, if any, append the current element to the array variable.

Expression reference:

variables('JSONkeys')[items('Apply_to_each')]
  • The final output array variable is the collection of first level keys present in JSON.

 

Note: Errors may occur because the current traversed property may not exist in JSON. You could ignore the error and set "Configure run after" of Compose action. It should continue to execute when Apply to each success or fails.

 

Image reference:

36.PNG

55.PNG

Flow run history:

56.PNG

Hope it helps.

 

Best Regards,

Community Support Team _ Barry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
3 REPLIES 3
in2217
Level: Powered On

Re: Iterating over JSON elements, pulling all first level keys?

I just figured that I could use Compose action to wrap list of JSON values in an array with array() function:

 

array(triggerBody()?['message']?['changes'])
 
but the array function just literally wraps the whole JSON as a single array item and this is not what I want.
Community Support Team
Community Support Team

Re: Iterating over JSON elements, pulling all first level keys?

Hi @in2217 ,

 

Please try the following method.

 

  • Initialize three variables, the first one is used to check if the key exists, and how many keys will be configured according to your own actual situation.
  • The second variable is used to store JSON Data that needs to be checked, and the third variable is used to store the first level keys.
  • Traverse checkList, using the current traversed element as the property name of JSON to get the property value, if any, append the current element to the array variable.

Expression reference:

variables('JSONkeys')[items('Apply_to_each')]
  • The final output array variable is the collection of first level keys present in JSON.

 

Note: Errors may occur because the current traversed property may not exist in JSON. You could ignore the error and set "Configure run after" of Compose action. It should continue to execute when Apply to each success or fails.

 

Image reference:

36.PNG

55.PNG

Flow run history:

56.PNG

Hope it helps.

 

Best Regards,

Community Support Team _ Barry
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
in2217
Level: Powered On

Re: Iterating over JSON elements, pulling all first level keys?

Hi @v-bacao-msft ,

thank you for elaborative suggestion.

 

Unfortunately it is impractical in my case because there are tens of keys in this original JSON and they are not static - we expect new additions with the time.

 

In the meanwhile I've come up with solution that would make use xpath function and an ability to iterate over XML elements, thanks to blog post by @John Liu. I'm not done yet, but the schetch goes like this:

 

1. Wrap original json in root node: 

2. Convert to xml:

3. Iterate over XML elements and populate an array of XMLs

4. Iterate over array and populate an array of JSON elements.

 

Thank you!

Helpful resources

Announcements
firstImage

Microsoft Business Applications Virtual Launch Event

Join us for an in-depth look at the new innovations across Dynamics 365 and the Microsoft Power Platform.

firstImage

Watch Sessions On Demand!

Continue your learning in our online communities.

Power Platform 2019 Release Wave 2 Plan

Power Platform 2019 Release Wave 2 Plan

Features releasing from October 2019 through March 2020

thirdimage

Flow Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

fifthimage

Microsoft Learn

Learn how to build the business apps that you need

sixthImage

Power Platform World Tour

Find out where you can attend!

seventhimage

Webinars & Video Gallery

Watch & learn from the Flow Community Video Gallery!

Top Kudoed Authors
Users Online
Currently online: 195 members 4,595 guests
Please welcome our newest community members: