title: How to create JSON from strings without creating a custom class using JSON.Net library
body: <p>I have this method in which I'm trying to create JSON string:</p>
  <pre><code>//my current file
  using Newtonsoft.Json;
  string key1 = &quot;FirstKey&quot;;
  string key2 = &quot;SecondKey&quot;;
  string key3 = &quot;ThirdKey&quot;;
  private string CreateJson(string val1,  string val2, string val3,string val4,  string val5, string val6)
  {
      //process the six arguments and three key-related member variables to create a JSON array
      //don't want to use JsonConvert.SerializeObject the way I'm doing below as it requires creating a class
  
      var configs = new List&lt;CustomClass&gt;
                           { new CustomClass{ FirstKey = val1,SecondKey= val2,ThirdKey= val3}
                              , new CustomClass{ FirstKey= val4,SecondKey= val5,ThirdKey = val6}
                          };
      var jsonData = JsonConvert.SerializeObject(configs);
     return jsonData;
  }
  
  //A class in another file
  public class CustomClass
  {
      public string FirstKey { get; set; }
      public string SecondKey{ get; set; }
      public string ThirdKey{ get; set; }
  
  }
  </code></pre>
  <p>I'm trying to create the JSON array using JSON.Net. The expected output is as below:</p>
  <pre><code>[{&quot;FirstKey&quot;:val1,&quot;SecondKey&quot;:val2,&quot;ThirdKey&quot;:val3}
  , {&quot;FirstKey&quot;:val4,&quot;SecondKey&quot;:val5,&quot;ThirdKey&quot;:val6}]
  </code></pre>
  <p>Here <code>val1</code> to <code>val6</code> values should get replaced by the argument values at run-time.</p>
  <p>Initially, since there were just three types of string key-value pairs, so I thought it would be pretty straightforward to create a JSON string simply by using string literals and appending then one after the other in JSON format. But soon I stumbled upon the world of escape characters which can deform a JSON string e.g. <code>\r</code>.</p>
  <p>I had been using JSON.Net library in the past simply to serialize and deserialize objects using <code>JSONConvert</code> class and I never cared and was completely unaware about this handling of escape characters by JSON.Net library does behind the scene for us to keep the JSON string valid.</p>
  <p>Anyways, coming back to my problem. I was able to solve my problem by creating a custom class having three properties <code>FirstKey</code>, <code>SecondKey</code>, and <code>ThirdKey</code>. Then, create an object of the class, then assign the values in arguments <code>val1</code> and <code>val2</code> to then and then use <code>JsonConvert.SerializeObject</code> API.</p>
  <p>I want a very simply way of creating JSON string using JSON.Net NuGet package without involving custom classes. Creating a class <code>CustomClass</code> altogether feels like an overhead here. I'm looking for something of sort of like <code>StringBuilder.Append</code> API if it is available in the JSON library I'm using. I'm not sure if I'm missing any of the APIs in JSON.Net.</p>