title: Object structure neccessary to serialize collection into arrays of JSON Objects
body: <p>I want to use JSON.Net to serialize a complex C#.net object into JSON.  The class structure is flexible (completely under my control), but JSON is needed in a specific format to communicate with another application.   I require JSON in the following format, but need assistance composing the classes and nested collections:</p>
  <pre><code>{
   &quot;Record_1&quot;: [  {  &quot;key_1&quot;: &quot;value_1&quot;, &quot;key_N&quot;: &quot;value_N&quot;},   {  &quot;key_1&quot;: &quot;value_1&quot;, &quot;key_N&quot;: &quot;value_N&quot;}],
   &quot;Record_2&quot;: [  {  &quot;key_1&quot;: &quot;value_1&quot; } ],
   &quot;Record_N&quot;: [  {  &quot;key_1&quot;: &quot;value_1&quot; } , { &quot;key_N&quot;:&quot;value_N&quot;}],
   … Other properties (simple string key value pairs)
  }
  </code></pre>
  <ul>
  <li>There will be 1 or more <em>Record</em> properties, the value of each <em>Record</em> is an array of objects.</li>
  <li>The <em>Record</em> property names are not known (I have a list of possibilities, but do not want to hardcode them into the application).</li>
  <li>There will be 1 or more objects in each array.</li>
  <li>The objects in the array will have 1 or more string key value pairs.  Again, property names are not known at compile time.</li>
  </ul>
  <hr />
  <p>Since the <em>Record</em> properties were unknown, I tried using a dictionary of objects (Dictionary&lt;string,object&gt;) annotated with [JsonExtensionData].  The value object in the dictionary was another collection. However, that results in string properties with object values:</p>
  <pre><code> &quot;Record_1&quot;: { key_1: [ value_1, value_2, value_N] }, &quot;Record_N&quot;: { key_1: [value_1]  }
  </code></pre>
  <p>Close, but not correct, the value of <em>Record</em> has to be an array.</p>
  <p>Next, I tried using a list of lists of objects, but that didn't work either because the variable names of the lists are returned as the property names:</p>
  <pre><code>  &quot;RecordSet&quot;: [
      {
        &quot;Records&quot;: [
          {
            &quot;Key_1&quot;: &quot;Value_1&quot;
          }
        ]
      },
      {
        &quot;Records&quot;: [
          {
            &quot;Key_1&quot;: &quot;Value_1&quot;,
          }
        ]
      }
    ]
  </code></pre>
  <p>So this does result in an array objects, but contents of the object are not in the correct form.</p>
  <p>I believe I need some combination of nested collections in my class to output the records in the required JSON format, but I don't know which.  Hopefully someone else has encountered this situation before.</p>
  <p>BTW: Currently, I'm directly writing to the JSON object to get the structure required. I'm hoping that there will be some performance increase in refactoring for serialization.  I'm not looking to just add complexity so I can say I'm following object oriented coding principles.  There will never be a need for deserialization of this JSON back into an object.</p>