Iterating through Jobject

Hi @Cormac,

Your Filed(“FieldName”).ToString log message should be set within body of first for each and not in the second for each loop. Your second for each will start parsing the “Values” key in your json and your Filed(“FieldName”).ToString will not be found for each iteration of your second for loop. That is the reason for the “object not set to reference of an instance” error message.

Since I cannot see your “Values” : [{ }] Json Array I am using an example with similar tree structure.

Lets say you want to get the values for
“FieldName” (Fields–>Every element may have a FieldName)
OR
“x” in Fields—>Values —> (x)

  {
   "Fields": [
       {
           "FieldName": "RockPaperX",
           "Values": [
               {
                   "x": "1"
               },
               {
                   "y": "2"
               }
           ]
       }
   ]
}

Here the first for loop is just to access the all values of Fields and second is to get all key value pairs of key named “Values”. You can then get the value for key “FieldName” OR “x” by using the following workflow.

All for each have the same argument type: image

You can also see in the log message that the flow iterates through the second element of “Values”, which only consists key “y” and but not key “x”. Nonetheless, the log message returns empty string as the value for key “x” . So while working with json keys you will always get a return back. It does not matter if the key exists or not in a given array. This may sometime cause challenges therefore it is good to know.

Files for your reference:
InputJSON.json (267 Bytes)
JSONObject.xaml (8.0 KB)

Hope this helps!

1 Like