title: Formatting output of Newtonsoft.Json.JsonConvert.SerializeObject(dataSet)
body: <p>I have a DataTable. When it's serialized into JSON with</p>
  
  <pre><code>Newtonsoft.Json.JsonConvert.SerializeObject(dataTable)
  </code></pre>
  
  <p>I get results in the following format:</p>
  
  <pre><code>[
      {
          "CLICK_KEY": 6254523744,
          "WEB_SERVER_KEY": 291,
          "PREV_CLICK_KEY": 0,
          "NEXT_CLICK_KEY": 0,
          "SESSION_KEY": 214981151,
          *more key value pairs*
      },
      {
          "CLICK_KEY": 6254523745,
          "WEB_SERVER_KEY": 291,
          "PREV_CLICK_KEY": 0,
          "NEXT_CLICK_KEY": 0,
          "SESSION_KEY": 214746780
          *more key value pairs*
      },
      *many more objects (for each row)*
  ]
  </code></pre>
  
  <p>Since I have many columns and many rows, the resulting JSON is huge.  This is mostly because column names are long and keep repeating for each row of data.</p>
  
  <p>Is there a way to change settings of Json.Net so that the resulting JSON string is reduced in size? For instance, by formatting output as:</p>
  
  <pre><code>{
      "NAMES": [
          "CLICK_KEY",
          "WEB_SERVER_KEY",
          "PREV_CLICK_KEY",
          "NEXT_CLICK_KEY",
          "SESSION_KEY",
          *more keys*
      ],
      "VALUES": [
          [6254523744, 291, 0, 0, 214981151, *more values*],
          [6254523745, 291, 0, 0, 214746780, *more values*],
          *many more arrays of values (for each row)*
      ]
  }
  </code></pre>
  
  <p>I will not need to deserialize this back into a table or another object, so a "one-way" solution would work.</p>
  
  <p>Thanks!</p>
  
  <p><strong>Update:</strong><br>
  I followed advice from @spender &amp; @TravisJ and transformed my <code>DataTable</code> into another type for which Newtonsoft.Json.JsonConvert.SerializeObject() outputs what I need. Note how I start at <code>DataSet</code> level so if it contains more than one <code>DataTable</code>, it will include each one in the array.</p>
  
  <pre><code>var converted = from x in dataSet.Tables.Cast&lt;DataTable&gt;()
                  select new
                  {
                    NAMES = x.Columns.Cast&lt;DataColumn&gt;().Select(l =&gt; l.Caption),
                    VALUES = x.Rows.Cast&lt;DataRow&gt;().Select(l =&gt; l.ItemArray)
                  };
  string jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(converted);
  </code></pre>