title: Array or object: how to use nlohmann::json for simple use cases?
body: <p>I want to use the nlohmann JSON library in order to read options from a JSON file.  Specifying options is optional, as reflected in the constructor in my code example.  I'm assuming the JSON structure is an object in its root.</p>
  <p>Unfortunately, I'm unable to use these options, because it is unclear to me how I can force the JSON structure to be an object.  What is worse, merely initializing a member variable with a JSON object <code>{}</code> (magically?) turns it into an array <code>[{}]</code>.</p>
  <pre class="lang-cpp prettyprint-override"><code>#include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;nlohmann/json.hpp&gt;
  
  class Example {
  public:
    explicit Example(const nlohmann::json&amp; options = nlohmann::json::object())
      : m_options{options}
    {
      std::clog &lt;&lt; options &lt;&lt; '\n' &lt;&lt; m_options &lt;&lt; '\n';
    }
  private:
    nlohmann::json m_options;
  };
  
  auto main() -&gt; int
  {
    Example x;
    Example y{nlohmann::json::object()};
    return EXIT_SUCCESS;
  }
  </code></pre>
  <p>This results in the following output.  Notice that we have to perform some ceremony in order to use an empty object as the default value (= empty settings), with <code>= nlohmann::json::object()</code>.  Also notice that the settings object changes its type as soon as we initialize the member value (!):</p>
  <pre><code>{}
  [{}]
  </code></pre>
  <p>My use use case is quite straightforward, but I'm unable to extract settings, unless I explicitly check whether the settings are an array or an object.</p>
  <p>Another thing that worries me is that incorrect code compiles without warning, e.g., code in which I use <code>x.value(&quot;y&quot;)</code> on a JSON array <code>x</code> containing an object with key <code>&quot;y&quot;</code>.  Only at run time do I discover that I should have done <code>x.at(0).value(&quot;y&quot;)</code> instead.</p>
  <p>In brief, the whole situation is quite surprising to me.  I must be missing something / I must be using this library in an unintended way?</p>