cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
Resolver I
Resolver I

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
Highlighted
Community Support
Community Support

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.

View solution in original post

3 REPLIES 3
Highlighted
Resolver I
Resolver I

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.
Highlighted
Community Support
Community Support

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.

View solution in original post

Highlighted
Resolver I
Resolver I

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 Ignite 2020

Check out the announcement of Power Platform content at Microsoft Ignite!

thirdImage

Experience what's new for Power Automate

Join us for an in-depth look at the new Power Automate features and capabilities at the free Microsoft Business Applications Launch Event.

firstImage

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors
Users online (7,884)