cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
in2217
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

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
in2217
Resolver I
Resolver I

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.

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

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
Process Advisor

Introducing Process Advisor

Check out the new Process Advisor community forum board!

MPA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS on Demand

Microsoft Business Applications Summit sessions

On-demand access to all the great content presented by the product teams and community members! #MSBizAppsSummit #CommunityRocks

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

Users online (21,523)