Iterating through Jobject

Hello,

I am currently trying to iterate through a Json object as per the example below. I am hitting the error “object not set to reference of an instance” when I try to run through the first For Each. Would anyone know what I am doing wrong here?

Hi @Cormac,
I’m not sure how your output is looking like but is this correct?
image
Filed instead of Field?

1 Like

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

Hey @jeevith - Thanks for your response. I have corrected the Log Message activities as per your suggestion but the issue that I am facing is that the bot/code can’t seem to find the “Fields” word inside the Json object. It is presenting the error "Object reference not set to instance of an object. This would tell me it can’t find “Fields”

“Fields” is present in the Json code however.

I am also Newtonsoft.Json.Linq as my argument type for both For Each activities

@Cormac,
Could you share / paste the contents of the json file? It makes it easier to debug :slight_smile:

1 Like

Hi @Cormac,

If you see the Json text, your Fields key is a child of ResultsDocument key. (Line 4)

  1. The solution is to use
    JsonObject("ResultsDocument")("Fields") in your first for loop.

  2. You can further use a second for loop to get the value of each field name

The XAML file : JsonParse.xaml (9.5 KB)

Hope this helped!

1 Like

That worked! Thanks so much for your help!

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.