title: Dynamically Parse Child Nodes in JSON
body: <p>I have a deserialized object that I want to dynamically loop through to return the related results. The response package looks like so:</p>
  <pre><code>{&quot;RatingResponse&quot;:
   {&quot;Success&quot;:&quot;true&quot;,
    &quot;Message&quot;:&quot;&quot;,
    &quot;QuoteID&quot;:&quot;57451&quot;,
    &quot;LoadNum&quot;:&quot;57451&quot;,
    &quot;Rates&quot;:
      {&quot;Rate&quot;:
          [
              {&quot;SCAC&quot;:&quot;test1&quot;,
               &quot;CarrierName&quot;:&quot;TEST1&quot;,
               &quot;TransitTime&quot;:&quot;1&quot;,
               &quot;ServiceLevel&quot;:&quot;D&quot;,
               &quot;TotalCost&quot;:&quot;1,031.82&quot;,
               &quot;ThirdPartyCharge&quot;:&quot;1,031.82&quot;,
               &quot;Accessorials&quot;:
                  {&quot;Accessorial&quot;:
                      [
                          {&quot;Code&quot;:&quot;400&quot;,
                          &quot;Cost&quot;:&quot;1,655.55&quot;,
                          &quot;Description&quot;:&quot;Freight&quot;
                          },
                          
                          {&quot;Code&quot;:&quot;DSC&quot;,
                          &quot;Cost&quot;:&quot;-952.77&quot;,
                          &quot;Description&quot;:&quot;Discount&quot;
                          },
                          
                          {&quot;Code&quot;:&quot;FUE&quot;,
                          &quot;Cost&quot;:&quot;329.04&quot;,
                          &quot;Description&quot;:&quot;Fuel Surcharge&quot;
                          }
                      ]
                  },
                  &quot;QuoteNumber&quot;:&quot;&quot;
              },
              
              {&quot;SCAC&quot;:&quot;test2&quot;,
              &quot;CarrierName&quot;:&quot;TEST2&quot;,
              &quot;TransitTime&quot;:&quot;1&quot;,
              &quot;ServiceLevel&quot;:&quot;D&quot;,
              &quot;TotalCost&quot;:&quot;1,031.82&quot;,
              &quot;ThirdPartyCharge&quot;:&quot;1,031.82&quot;,
              &quot;Accessorials&quot;:
                  {&quot;Accessorial&quot;:
                      [
                          {&quot;Code&quot;:&quot;400&quot;,
                          &quot;Cost&quot;:&quot;1,655.55&quot;,
                          &quot;Description&quot;:&quot;Freight&quot;
                          },
                          
                          {&quot;Code&quot;:&quot;DSC&quot;,
                          &quot;Cost&quot;:&quot;-952.77&quot;,
                          &quot;Description&quot;:&quot;Discount&quot;
                          },
                          
                          {&quot;Code&quot;:&quot;FUE&quot;,
                          &quot;Cost&quot;:&quot;329.04&quot;,
                          &quot;Description&quot;:&quot;Fuel Surcharge&quot;
                          }
                      ]
                  },
                  &quot;QuoteNumber&quot;:&quot;&quot;
              }
          ]
      },
      &quot;AverageTotalCost&quot;:&quot;1,031.82&quot;
     }
   }
  </code></pre>
  <p>I have parsed the response data so that there is less information to work with, especially since I only need the Accessorial Costs. The parsed response looks like</p>
  <pre><code> [
   {
  &quot;SCAC&quot;: &quot;test1&quot;,
  &quot;CarrierName&quot;: &quot;TEST1&quot;,
  &quot;TransitTime&quot;: &quot;1&quot;,
  &quot;ServiceLevel&quot;: &quot;D&quot;,
  &quot;TotalCost&quot;: &quot;1,031.82&quot;,
  &quot;ThirdPartyCharge&quot;: &quot;1,031.82&quot;,
  &quot;Accessorials&quot;: {
    &quot;Accessorial&quot;: [
      {
        &quot;Code&quot;: &quot;400&quot;,
        &quot;Cost&quot;: &quot;1,655.55&quot;,
        &quot;Description&quot;: &quot;Freight&quot;
      },
      {
        &quot;Code&quot;: &quot;DSC&quot;,
        &quot;Cost&quot;: &quot;-952.77&quot;,
        &quot;Description&quot;: &quot;Discount&quot;
      },
      {
        &quot;Code&quot;: &quot;FUE&quot;,
        &quot;Cost&quot;: &quot;329.04&quot;,
        &quot;Description&quot;: &quot;Fuel Surcharge&quot;
      }
    ]
  },
  &quot;QuoteNumber&quot;: &quot;&quot;
   },
   {
  &quot;SCAC&quot;: &quot;test2&quot;,
  &quot;CarrierName&quot;: &quot;TEST2&quot;,
  &quot;TransitTime&quot;: &quot;1&quot;,
  &quot;ServiceLevel&quot;: &quot;D&quot;,
  &quot;TotalCost&quot;: &quot;1,031.82&quot;,
  &quot;ThirdPartyCharge&quot;: &quot;1,031.82&quot;,
  &quot;Accessorials&quot;: {
    &quot;Accessorial&quot;: [
      {
        &quot;Code&quot;: &quot;400&quot;,
        &quot;Cost&quot;: &quot;1,655.55&quot;,
        &quot;Description&quot;: &quot;Freight&quot;
      },
      {
        &quot;Code&quot;: &quot;DSC&quot;,
        &quot;Cost&quot;: &quot;-952.77&quot;,
        &quot;Description&quot;: &quot;Discount&quot;
      },
      {
        &quot;Code&quot;: &quot;FUE&quot;,
        &quot;Cost&quot;: &quot;329.04&quot;,
        &quot;Description&quot;: &quot;Fuel Surcharge&quot;
      }
    ]
  },
  &quot;QuoteNumber&quot;: &quot;&quot;
  }
  ]
  </code></pre>
  <p>The problem I am facing is that I will never know how many Rate items will come back in the response data, nor will I know the exact amount of Accessorial Costs. I'm hoping to capture the Rate child node counts and the Accessorial child node counts per Rate. Here's what I have so far.</p>
  <pre><code>   Root rootObject = Newtonsoft.Json.JsonConvert.DeserializeObject&lt;Root&gt;(responseFromServer);
     //rate stores the parsed response data
     JArray rate = (JArray)JObject.Parse(responseFromServer)[&quot;RatingResponse&quot;][&quot;Rates&quot;][&quot;Rate&quot;];
     var rate2 = rate.ToString();
  
     //this for loop works as expected. it grabs the number of Rate nodes (in this example, 2)
     for (int i = 0; i &lt; rate.Count(); i++)
         {
            dynamic test2 = rate[i];
            //this is where I'm struggling
            dynamic em = (JArray)JObject.Parse(test2)[&quot;Accessorials&quot;][&quot;Accessorial&quot;].Count();
               for (int j = 0; j &lt; em; j++)
                   {
                      string test3 = test2.Accessorials.Accessorial[j].Cost;
                      System.IO.File.AppendAllText(logPath, Environment.NewLine + test3 + Environment.NewLine);
                          }
                  }
  </code></pre>
  <p>I apologize in advance for the bad formatting and odd variable names - I'm obviously still testing the functionality, so I've been using random variables.</p>
  <p>Where I'm struggling (as notated above) is getting to the Accessorial node to count how many items are in its array. I was thinking I could parse the first array (starting with SCAC data) and extend down to the Accessorial node, but I'm not having any luck.</p>
  <p>Any help is GREATLY appreciated, especially since I am new to this type of code and have spent the majority of the day trying to resolve this.</p>