title: Dynamically create JSON object for datareader results, regardless of what they look like?
body: <p>Right now I'm using <code>Newtonsoft.json</code> with my <code>OdbcConnection</code> and manually creating objects for each query I run.  It looks something like this:</p>
  
  <p>Classes:</p>
  
  <pre><code>public class payload
  {
      public string id;
      public string type;
      public DateTime timestmap;
      public object data;
  }
  public class resultPhrLastTime
  {
      public string facilityId;
      public string type;
      public string time;
  } 
  </code></pre>
  
  <p>Code:</p>
  
  <pre><code>payload result = new payload();
  var resultList = new List&lt;resultPhrLastTime&gt;();
  result.id = "someid";
  
  //connection stuff
  
          while (reader.Read())
          {
              var t = new resultPhrLastTime();
              //if (verbose) log(reader[0].ToString());
              t.facilityId = reader[0].ToString();
              t.type = reader[1].ToString();
              t.time = reader[2].ToString();
              resultList.Add(t);
          }
  
      result.data = resultList;
      result.timestmap = DateTime.Now;
      result.type = "complex";
      string output = JsonConvert.SerializeObject(result);
  </code></pre>
  
  <p>This works fine, but every time I add a new query to my app (of which there will be many) I have to create a new custom class if the data looks different at all.</p>
  
  <p>I would like to find a way to convert the entire <code>reader</code> object to JSON regardless of the format, so it may look like this:</p>
  
  <blockquote>
    <p>SQL Result:</p>
  
  <pre><code>2814814
  </code></pre>
    
    <p>JSON:</p>
  
  <pre><code>result: {
     timestamp: 2016-09-10 8:15,
     data: { '2814814' }
  }
  </code></pre>
  </blockquote>
  
  <p>or it may look like this:</p>
  
  <blockquote>
    <p>SQL Result:</p>
  
  <pre><code>Apple  | 59    
  Orange | 17
  </code></pre>
    
    <p>JSON:</p>
  
  <pre><code>result: {
     timestamp: 2016-09-10 8:15,
     data: {[ 
        'Apple':'59',
        'Orange':'17'
     ]}
  }
  </code></pre>
  </blockquote>
  
  <p>Or there could be 5 columns...</p>
  
  <p>Is there a way to do this?</p>