title: Looping through a decoded JSON of nested elements to construct a set of requests
body: <p>I’ve read various SO posts relating to decoding JSON content, but as yet none of them seem to cover my situation.</p>
  <p>I’m making an api call to a service that returns the following json structure, static version provided below:</p>
  <pre><code>local jsonstatic = [[
  {
      &quot;probe_no&quot;: 2,
      &quot;probe_list&quot;:[
          {
              &quot;probe_id&quot;: 1,
              &quot;probe_type&quot;: 0,
              &quot;sensor_name&quot;: [&quot;Int. Temp1&quot;,null],
              &quot;unit_type&quot;: [2,16],
              &quot;alert_type&quot;: [2,3],
              &quot;warn_range&quot;: [[18.00,37.00],[50.00,0.00]],
              &quot;down_range&quot;: [[15.00,41.00],[60.00,0.00]],
              &quot;alert_setting&quot;: [[0,0,0,0],[0,0,0,0]],
              &quot;output&quot;: [null,null],
              &quot;output_state&quot;: [0,0],
              &quot;default_state&quot;: [null,null]
              
          },
          {
              &quot;probe_id&quot;: 2,
              &quot;probe_type&quot;: 1,
              &quot;sensor_name&quot;: [&quot;Ext. Temp1&quot;,&quot;Humidity1&quot;,&quot;Dew Point1&quot;],
              &quot;unit_type&quot;: [2,4,2],
              &quot;alert_type&quot;: [2,2,2],
              &quot;warn_range&quot;: [[18.00,25.00],[45.00,65.00],[18.00,25.00]],
              &quot;down_range&quot;: [[15.00,29.00],[40.00,70.00],[15.00,29.00]],
              &quot;alert_setting&quot;: [[0,0,0,0],[0,0,0,0],[0,0,0,0]],
              &quot;output&quot;: [null,null,null],
              &quot;output_state&quot;: [0,0,0],
              &quot;default_state&quot;: [null,null,null]
          },
          {
              &quot;probe_id&quot;: 3,
              &quot;probe_type&quot;: 99,
              &quot;sensor_name&quot;: [null],
              &quot;unit_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;alert_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;warn_range&quot;: [0,0],
              &quot;down_range&quot;: [0,0],
              &quot;alert_setting&quot;: [0],
              &quot;output&quot;: [null],
              &quot;output_state&quot;: [null],
              &quot;default_state&quot;: [null]
          },
          {
              &quot;probe_id&quot;: 4,
              &quot;probe_type&quot;: 99,
              &quot;sensor_name&quot;: [null],
              &quot;unit_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;alert_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;warn_range&quot;: [0,0],
              &quot;down_range&quot;: [0,0],
              &quot;alert_setting&quot;: [0],
              &quot;output&quot;: [null],
              &quot;output_state&quot;: [null],
              &quot;default_state&quot;: [null]
          },
          {
              &quot;probe_id&quot;: 5,
              &quot;probe_type&quot;: 99,
              &quot;sensor_name&quot;: [null],
              &quot;unit_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;alert_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;warn_range&quot;: [0,0],
              &quot;down_range&quot;: [0,0],
              &quot;alert_setting&quot;: [0],
              &quot;output&quot;: [null],
              &quot;output_state&quot;: [null],
              &quot;default_state&quot;: [null]
          },
          {
              &quot;probe_id&quot;: 6,
              &quot;probe_type&quot;: 99,
              &quot;sensor_name&quot;: [null],
              &quot;unit_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;alert_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;warn_range&quot;: [0,0],
              &quot;down_range&quot;: [0,0],
              &quot;alert_setting&quot;: [0],
              &quot;output&quot;: [null],
              &quot;output_state&quot;: [null],
              &quot;default_state&quot;: [null]
          },
          {
              &quot;probe_id&quot;: 7,
              &quot;probe_type&quot;: 99,
              &quot;sensor_name&quot;: [null],
              &quot;unit_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;alert_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;warn_range&quot;: [0,0],
              &quot;down_range&quot;: [0,0],
              &quot;alert_setting&quot;: [0],
              &quot;output&quot;: [null],
              &quot;output_state&quot;: [null],
              &quot;default_state&quot;: [null]
          },
          {
              &quot;probe_id&quot;: 8,
              &quot;probe_type&quot;: 99,
              &quot;sensor_name&quot;: [null],
              &quot;unit_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;alert_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;warn_range&quot;: [0,0],
              &quot;down_range&quot;: [0,0],
              &quot;alert_setting&quot;: [0],
              &quot;output&quot;: [null],
              &quot;output_state&quot;: [null],
              &quot;default_state&quot;: [null]
          },
          {
              &quot;probe_id&quot;: 9,
              &quot;probe_type&quot;: 99,
              &quot;sensor_name&quot;: [null],
              &quot;unit_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;alert_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;warn_range&quot;: [0,0],
              &quot;down_range&quot;: [0,0],
              &quot;alert_setting&quot;: [0],
              &quot;output&quot;: [null],
              &quot;output_state&quot;: [null],
              &quot;default_state&quot;: [null]
          },
          {
              &quot;probe_id&quot;: 10,
              &quot;probe_type&quot;: 99,
              &quot;sensor_name&quot;: [null],
              &quot;unit_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;alert_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;warn_range&quot;: [0,0],
              &quot;down_range&quot;: [0,0],
              &quot;alert_setting&quot;: [0],
              &quot;output&quot;: [null],
              &quot;output_state&quot;: [null],
              &quot;default_state&quot;: [null]
          },
          {
              &quot;probe_id&quot;: 11,
              &quot;probe_type&quot;: 99,
              &quot;sensor_name&quot;: [null],
              &quot;unit_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;alert_type&quot;: [1,1,1,1,1,1,1,1,1,1,1,1],
              &quot;warn_range&quot;: [0,0],
              &quot;down_range&quot;: [0,0],
              &quot;alert_setting&quot;: [0],
              &quot;output&quot;: [null],
              &quot;output_state&quot;: [null],
              &quot;default_state&quot;: [null]
          }
      ]
      }
  ]]
  </code></pre>
  <p>I can retrieve specific values using the following:</p>
  <pre><code>local json = require(&quot;dkjson&quot;)
  local d = json.decode(jsonstatic)
  print(d.probe_list[1].probe_id) -- 1
  print(d.probe_list[1].sensor_name[1]) -- &quot;Int. Temp1&quot;
  print(d.probe_list[1].warn_range[1][1]) -- 18
  print(d.probe_list[1].warn_range[1][2])-- 37
  print(d.probe_list[1].probe_type)-- 0
  </code></pre>
  <p>But what I need to do is loop through that json to extract and process a selection of variables within each section of that JSON ?</p>
  <p>So far I’ve made god-knows-how-many-attempts over the last few days at this, and while I can loop through to extract some of the higher level values in that JSON, but when it comes to the nested elements I end up with a reduced list (sharing one example below):</p>
  <pre><code>
  for k, v in pairs(d.probe_list) do
      for k1, v1 in pairs(v) do
      --print(k,k1, v1)
          if type(v1) == &quot;table&quot; then
              for k2, v2 in pairs (v1) do  
              --  print (k, k1, v1, k2, v2) 
                      if type(v2) == &quot;table&quot; then
                          for k3, v3 in pairs (v2) do  
                              print (k, v, k1, v1, k2, v2, k3, v3)
                          end
                      end 
              end
          end
      end
  end
  
  </code></pre>
  <p>How can I get all values to play out on one line/row. Is it even possible to extract such a selection of values all together via a loop?</p>
  <p>To avoid complicating matters by trying to explain the eventual command I need to populate with these values, to keep it simple and help me learn, let’s say I wanted to loop through the json and build a sentence out of each sensor/section?</p>
  <p>How would I do something like the following..</p>
  <pre><code>&quot;The probe No is “ .. ProbeNo .. “ which is called “ .. probe name .. &quot;, which is set to warn you when it’s value is recorded below  &quot; .. warn1 .. &quot; or above &quot; .. warn2 .. &quot; etc.”
  </code></pre>
  <p>Please note:  I ideally need the sensor name of each element to act as the unique identifier, so is it possible to construct a sentence for each one? Which means for some it has 3 sensors in one section.</p>
  <h2>Update</h2>
  <p>I think I can construct something workable using the following logic/approach, but it’s likely to be quite long - hence the reason for this post is to learn if it’s possible to do something more dynamic using the decoded json and a loop</p>
  <pre><code>function findprode(probename)
      if d.probe_list[1].sensor_name[1] == probename then
      print(&quot;found &quot; ..probename.. &quot;, its probe ID = &quot;.. d.probe_list[1].probe_id .. &quot; it's warning values are lower than &quot; ..d.probe_list[1].warn_range[1][1].. &quot; or higher than &quot; ..d.probe_list[1].warn_range[1][2])
      elseif d.probe_list[2].sensor_name[1] == probename then
      print(&quot;found &quot; ..probename.. &quot;, its probe ID = &quot;.. d.probe_list[2].probe_id.. &quot; it's warning values are lower than &quot; ..d.probe_list[2].warn_range[1][1].. &quot; or higher than &quot; ..d.probe_list[2].warn_range[1][2])
      elseif d.probe_list[3].sensor_name[1] == probename then
      print(&quot;found &quot; ..probename.. &quot;, its probe ID = &quot;.. d.probe_list[3].probe_id.. &quot; it's warning values are lower than &quot; ..d.probe_list[3].warn_range[1][1].. &quot; or higher than &quot; ..d.probe_list[3].warn_range[1][2])
      elseif d.probe_list[4].sensor_name[1] == probename then
      print(&quot;found &quot; ..probename.. &quot;, its probe ID = &quot;.. d.probe_list[4].probe_id.. &quot; it's warning values are lower than &quot; ..d.probe_list[4].warn_range[1][1].. &quot; or higher than &quot; ..d.probe_list[4].warn_range[1][2])
      else
      print(probename.. &quot; NOT FOUND&quot;)
      end
  end
  findprode(&quot;Int. Temp1&quot;)
  </code></pre>