title: nlohmann::json array of objects conversion into memory structure
body: <p>I am using the <a href="https://github.com/nlohmann/json" rel="nofollow noreferrer">nlohmann JSON C++ library</a> to read a JSON file with this structure:</p>
  <pre class="lang-json prettyprint-override"><code>{
    &quot;Main&quot;: [
      {
        &quot;obj1&quot;: &quot;bar&quot;
      },
      {
        &quot;obj2&quot;: &quot;foo&quot;
      }
    ]
  }
  </code></pre>
  <p>The main object with key known to the user <code>&quot;Main&quot;</code> contains an array of objects with unknown key names.</p>
  <p>I want to transfer the JSON object in the following program to a C++ structure.  How could this be done?</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;nlohmann/json.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  using json = nlohmann::json;
  
  int main()
  {
    std::ifstream ifs(&quot;../test/test_2.json&quot;);
    json js = json::parse(ifs);
  
    for (json::iterator it = js.begin(); it != js.end(); ++it) 
    {
      std::cout &lt;&lt; it.key() &lt;&lt; &quot; :\n&quot;;
      std::cout &lt;&lt; it.value() &lt;&lt; &quot;\n&quot;;
    }
  
    if (js.contains(&quot;Main&quot;))
    {
      json a = js[&quot;Main&quot;];
      for (size_t idx = 0; idx &lt; a.size(); ++idx)
      {
        json o = a.at(idx);
        std::cout &lt;&lt; o &lt;&lt; &quot;\n&quot;;
      }
    }
    return 0;
  }
  </code></pre>
  <p>Output:</p>
  <pre class="lang-none prettyprint-override"><code>Main :
  [{&quot;obj1&quot;:&quot;bar&quot;},{&quot;obj2&quot;:&quot;foo&quot;}]
  {&quot;obj1&quot;:&quot;bar&quot;}
  {&quot;obj2&quot;:&quot;foo&quot;}
  </code></pre>