title: Using JSON.NET to access JSON attributes to create a C# dictionary
body: <p>I want to convert what is ultimately a dictionary in JSON to a C# dictionary without much ado.<br>
  Am I barking up the wrong tree by using the JSON.NET library here? The JArray class doesn't want to give me anything to access the attribute (only the value) ie it tells me the value but never the "key".<br>
  I can't quite believe no one else would find this limiting, so am assuming I'm missing something. My muddled attempt is this:</p>
  
  <p>Given this json:  </p>
  
  <pre><code>{
   "appSettings" : [
      {"rows": "5"},
      {"columns" : "7"}
    ]
  }
  </code></pre>
  
  <p>I would like to select this into a dictionary like this:</p>
  
  <pre><code>var dict = jsonObject["appSettings"].Select(s =&gt; new 
  {
      key = s.Name,    // wish this property existed
      value = s.Value  // wish this property existed
  }).ToDictionary(s =&gt; s.key, s =&gt; s.value);
  </code></pre>
  
  <p>This is my UnitTest:</p>
  
  <pre><code>[Test]
  public void CanLoadJsonAppSettings()
  {
  var json = @"
      ""{appSettings"" : [ 
        {""ViewRows"" : ""1""},
        {""ViewColumns"" : ""2""}
      ]}";
      var dict = CreateJsonDictionary(json);
      Assert.That(dict.Count, Is.EqualTo(2));
  }
  
  public CreateJsonDictionary(string jsonText)
  {
    var jsonObject = JObject.Parse(jsonText);
  
    return jsonObject["appSettings"].Select(s =&gt; new 
    { 
      key = s.Name,
      value = s.Value
    }).ToDictionary(s =&gt; s.key, s =&gt; s.value);
  }
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong>: Thanks to @jim, we are a little closer.
  For completeness, I will document the slightly awkward step I needed in order to get at the object I needed:</p>
  
  <p>I had to change my JSON.
  Instead of using an array (as in the code above) I used a more simple/truer dictionary:</p>
  
  <pre><code>var json = @"
  { 
    ""appSettings"" : { 
      ""ViewRows""    : ""1"",
      ""ViewColumns"" : ""2""
      }
  }";
  </code></pre>
  
  <p>Then I had to <strong>Parse</strong>, get a JSON <em>JObject</em>, then convert back to a string, and then <strong>Deserialize</strong>:</p>
  
  <pre><code>var jo = JObject.Parse(jsonText);
  var appSettings = jo["appSettings"];
  var appSettings = JsonConvert.DeserializeObject&lt;Dictionary&lt;string, string&gt;&gt;(appSettings.ToString());
  </code></pre>
  
  <p>So part of my problem, was getting JSON confused.
  Even so, if there is a more elegant way to do this, I'm all ears.</p>
  
  <p><strong>EDIT2</strong>: I still had to solve the original problem above, converting a JSON array to a dictionary. Once my JSON is corrected to contain proper name/value pairs:</p>
  
  <pre><code>"connectionStrings": [
  {"name" : "string1", "value" : "value1"},
  {"name" : "string2", "value" :"value2"},
  ]
  </code></pre>
  
  <p>This is the code that solved it (nb it looks a lot like my original attempt):</p>
  
  <pre><code>var jsonObj = JObject.Parse(jsonText);
  var conStrings = jsonObj.Properties().Select(s =&gt; 
    new {
    key = s.Name,
    value = s.Value.ToString()
  }).ToDictionary(s =&gt; s.key, s =&gt; s.value);
  </code></pre>
  
  <p>And this only works if you have no other arrays.</p>