title: Parse JSON with numeric key c#
body: <p>I have an issue where an API i am trying to consume in .NET is using numeric keys. I have provided a sample of the JSON below.</p>
  <pre><code>{
  &quot;com&quot;: {
              &quot;categories&quot;: [
                  &quot;gTLD&quot;,
                  &quot;Popular&quot;
              ],
              &quot;addons&quot;: {
                  &quot;dns&quot;: true,
                  &quot;email&quot;: true,
                  &quot;idprotect&quot;: true
              },
              &quot;group&quot;: &quot;hot&quot;,
              &quot;register&quot;: {
                  &quot;1&quot;: &quot;11.99&quot;,
                  &quot;2&quot;: &quot;23.98&quot;,
                  &quot;3&quot;: &quot;35.97&quot;,
                  &quot;4&quot;: &quot;47.96&quot;,
                  &quot;5&quot;: &quot;59.95&quot;,
                  &quot;6&quot;: &quot;71.94&quot;,
                  &quot;7&quot;: &quot;83.93&quot;,
                  &quot;8&quot;: &quot;95.92&quot;,
                  &quot;9&quot;: &quot;107.91&quot;,
                  &quot;10&quot;: &quot;119.90&quot;
              },
              &quot;transfer&quot;: {
                  &quot;1&quot;: &quot;11.99&quot;,
                  &quot;2&quot;: &quot;23.98&quot;,
                  &quot;3&quot;: &quot;35.97&quot;,
                  &quot;4&quot;: &quot;47.96&quot;,
                  &quot;5&quot;: &quot;59.95&quot;,
                  &quot;6&quot;: &quot;71.94&quot;,
                  &quot;7&quot;: &quot;83.93&quot;,
                  &quot;8&quot;: &quot;95.92&quot;,
                  &quot;9&quot;: &quot;107.91&quot;,
                  &quot;10&quot;: &quot;119.90&quot;
              },
              &quot;renew&quot;: {
                  &quot;1&quot;: &quot;11.99&quot;,
                  &quot;2&quot;: &quot;23.98&quot;,
                  &quot;3&quot;: &quot;35.97&quot;,
                  &quot;4&quot;: &quot;47.96&quot;,
                  &quot;5&quot;: &quot;59.95&quot;,
                  &quot;6&quot;: &quot;71.94&quot;,
                  &quot;7&quot;: &quot;83.93&quot;,
                  &quot;8&quot;: &quot;95.92&quot;,
                  &quot;9&quot;: &quot;107.91&quot;,
                  &quot;10&quot;: &quot;119.90&quot;
              },
              &quot;grace_period&quot;: {
                  &quot;days&quot;: 30,
                  &quot;price&quot;: &quot;£0.00&quot;
              },
              &quot;redemption_period&quot;: null
          }
      }
  </code></pre>
  <p>I am trying to extract the register prices out of the JSON in Razor to use in a table, however i just can't seem to access the values.</p>
  <p>The Razor code:</p>
  <pre><code>var client = new RestClient(&quot;JSON_URL&quot;);
  IRestResponse response = client.Execute(new RestRequest());
  var o = JObject.Parse(response.Content);
  
  @foreach (JProperty child in o[&quot;pricing&quot;])
  {
      &lt;strong&gt;@child.Name&lt;/strong&gt;
      foreach (JObject ext in child.Children())
      {
          foreach (var property in ext[&quot;register&quot;].Select(f =&gt; f.ToObject&lt;GetDomainPricing&gt;()))
          {
              @property
          }
      }
  }
  </code></pre>
  <p>And the associated Model:</p>
  <pre><code>namespace TH.Models
  {
      public class GetDomainPricing
      {
          public string Price { get; set; }
      }
  }
  </code></pre>
  <p>After a week of battling with this error, the message i have ended up on is:</p>
  <pre><code>Newtonsoft.Json.JsonSerializationException: Unexpected token while deserializing object: PropertyName. Path 'pricing.com.register.1', line 1, position 243.
  </code></pre>
  <p>So at this point i am in need of some help, i don't have access to the JSON to change its format, but a link to the documentation is below:
  <a href="https://developers.whmcs.com/api-reference/gettldpricing/" rel="nofollow noreferrer">GetTldPricing - WHMCS Developer Documentation</a></p>
  <p>All their examples are using PHP which looks like it doesn't have this issue.</p>
  <hr />
  <h1>Solution</h1>
  <p>For anyone else trying to work out how to parse dynamic JSON with numeric keys, my answer is below.</p>
  <p>Model:</p>
  <pre><code>using System.Collections.Generic;
  using Newtonsoft.Json;
  
  namespace TerrabitHost.Models
  {
      public class Addons
      {
          public bool dns { get; set; }
          public bool email { get; set; }
          public bool idprotect { get; set; }
      }
  
      public class GracePeriod
      {
          public int days { get; set; }
          public string price { get; set; }
      }
  
      public class Pricing
      {
          public Extensions Tld { get; set; }
      }
      public class Extensions
  public class Extensions
  {
      public List&lt;string&gt; categories { get; set; }
      public Addons addons { get; set; }
      public string group { get; set; }
      public Prices register { get; set; }
      public Prices transfer { get; set; }
      public Prices renew { get; set; }
      public GracePeriod grace_period { get; set; }
      public object redemption_period { get; set; }
  }
  
      public class Register
      {
          [JsonProperty(PropertyName = &quot;1&quot;)]
          public string One { get; set; }
          [JsonProperty(PropertyName = &quot;2&quot;)]
          public string Two { get; set; }
          [JsonProperty(PropertyName = &quot;3&quot;)]
          public string Three { get; set; }
      }
  
      public class GetDomainPricing
      {
          public Pricing[] Pricing { get; set; }
      }
      public class JsonBody
      {
          public Dictionary&lt;string, Extensions&gt; Pricing { get; set; }
      }
  }
  </code></pre>
  <p>Razor View:</p>
  <pre><code>@{
      var client = new RestClient(&quot;JSON_URL&quot;);
      IRestResponse response = client.Execute(new RestRequest());
      JsonBody json = JsonConvert.DeserializeObject&lt;JsonBody&gt;(response.Content);
  }
  
  @foreach (var ext in json.Pricing)
  {
  &lt;strong&gt;@ext.Key&lt;/strong&gt;
  @ext.Value.register.One);
  }
  </code></pre>