title: Fetch Zendesk Tickets into SQL (JSON Handling)
body: <p>In the Zendesk Developer Blog i found a helpfull Entry(<a href="https://developer.zendesk.com/blog/getting-and-creating-tickets-with-.net" rel="nofollow">https://developer.zendesk.com/blog/getting-and-creating-tickets-with-.net</a>).</p>
  
  <p>I used the Code from the Article in an C# ConsoleApp for testing:</p>
  
  <pre><code>using System;
  using ConsoleApplication1.Properties;
  using Newtonsoft.Json.Linq;
  using RestSharp;
  using RestSharp.Authenticators;
  
  namespace ConsoleApplication1
  {
      public class Ticket
      {
          public int id { get; set; }
          public string url { get; set; }
          public string external_id { get; set; }
          public string type { get; set; }
          public string subject { get; set; }
          public string description { get; set; }
          public string priority { get; set; }
          public string status { get; set; }
          public string recipient { get; set; }
          public int requester_id { get; set; }
          public int submitter_id { get; set; }
          public int assignee_id { get; set; }
          public int organization_id { get; set; }
          public int group_id { get; set; }
      }
  
      internal class Program
      {
          private static readonly string requestUri = "https://xxx.zendesk.com/";
          private static readonly string username = Settings.Default.Username;
          private static readonly string password = Settings.Default.Password;
  
          private static void Main(string[] args)
          {
              var client = new RestClient(requestUri);
              client.Authenticator = new HttpBasicAuthenticator(username, password);
              var request = new RestRequest("/api/v2/tickets.json", Method.GET);
              client.AddDefaultHeader("Accept", "application/json");
  
              var response = client.Execute(request);
              var content = response.Content;
  
              var ticket = JObject.Parse(content);
  
              foreach (var pair in ticket)
              {
                  Console.WriteLine("{0}: {1}", pair.Key, pair.Value);
              }
          }
      }
  }
  </code></pre>
  
  <p>It works, and i receive the following Data:</p>
  
  <pre><code>tickets: [
    {
      "url": "xxx.zendesk.com/api/v2/tickets/1.json",
      "id": 1,
      "external_id": null,
      "via": {
        "channel": "sample_ticket",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2015-10-12T12:08:25Z",
      "updated_at": "2015-10-12T18:15:13Z",
      "type": "incident",
      "subject": "Beispielticket: erster Blick auf das Ticket",
      "raw_subject": "Beispielticket: erster Blick auf das Ticket",
      "description": "Hallo xxx,\n\nE-Mails, Chats, Voicemails und Tweets werden in Zendesk in Form von Tickets erfasst. Geben Sie oben Ihre Antwort
  ein und klicken Sie dann auf "Einreichen", um sie zu senden. Um auszuprobieren, wie eine E-Mail zu einem Ticket wird, senden Sie eine Nachricht an sup
  port@xxx.zendesk.com.\n",
      "priority": "normal",
      "status": "open",
      "recipient": null,
      "requester_id": 1371652071,
      "submitter_id": 1371651941,
      "assignee_id": null,
      "organization_id": null,
      "group_id": 24976891,
      "collaborator_ids": [],
      "forum_topic_id": null,
      "problem_id": null,
      "has_incidents": false,
      "due_at": null,
      "tags": [
        "zendesk-beispiel"
      ],
      "custom_fields": [
        {
          "id": 28089621,
          "value": null
        },
        {
          "id": 28087191,
          "value": null
        },
        {
          "id": 28087201,
          "value": null
        }
      ],
      "satisfaction_rating": null,
      "sharing_agreement_ids": [],
      "fields": [
        {
          "id": 28089621,
          "value": null
        },
        {
          "id": 28087191,
          "value": null
        },
        {
          "id": 28087201,
          "value": null
        }
      ],
      "brand_id": 782281
    },
    {
      "url": "xxx.zendesk.com/api/v2/tickets/2.json",
      "id": 2,
      "external_id": null,
      "via": {
        "channel": "web",
        "source": {
          "from": {},
          "to": {},
          "rel": null
        }
      },
      "created_at": "2015-10-12T20:26:13Z",
      "updated_at": "2015-10-12T20:26:13Z",
      "type": "question",
      "subject": "test",
      "raw_subject": "test",
      "description": "test 1234",
      "priority": "normal",
      "status": "open",
      "recipient": null,
      "requester_id": 1371651941,
      "submitter_id": 1371651941,
      "assignee_id": 1371651941,
      "organization_id": 157600651,
      "group_id": 24976891,
      "collaborator_ids": [],
      "forum_topic_id": null,
      "problem_id": null,
      "has_incidents": false,
      "due_at": null,
      "tags": [
        "jpa"
      ],
      "custom_fields": [
        {
          "id": 28089621,
          "value": "jpa"
        },
        {
          "id": 28087191,
          "value": null
        },
        {
          "id": 28087201,
          "value": null
        }
      ],
      "satisfaction_rating": null,
      "sharing_agreement_ids": [],
      "fields": [
        {
          "id": 28089621,
          "value": "jpa"
        },
        {
          "id": 28087191,
          "value": null
        },
        {
          "id": 28087201,
          "value": null
        }
      ],
      "brand_id": 782281
    }
  ]
  next_page:
  previous_page:
  count: 2
  </code></pre>
  
  <p>But now i did not know how to load the received data in Ticket Objects.</p>
  
  <p>I Need something like that:</p>
  
  <pre><code>foreach(Ticket t in [Received Data])
  {
      ... do something
  }
  </code></pre>
  
  <p>to replace the "Console.WriteLine("{0}: {1}", pair.Key, pair.Value);" part.</p>
  
  <p>These are my first steps in JSON, some how-to or first step link could be helpfull too.</p>
  
  <p>(tried to get hints on <a href="http://www.newtonsoft.com/json" rel="nofollow">http://www.newtonsoft.com/json</a> but without sucess)</p>
  
  <p>Update 1:</p>
  
  <p>After the Comment from dbc (thx, for the very helpful Link) i testet a Little bit more. The variable "content":</p>
  
  <pre><code>var content = response.Content;
  </code></pre>
  
  <p>contains a Valid JSON string:</p>
  
  <pre><code>{
      "tickets": [
          {
              "url": "https://xxx.zendesk.com/api/v2/tickets/2.json",
              "id": 2,
              "external_id": null,
              "via": {
                  "channel": "web",
                  "source": {
                      "from": {},
                      "to": {},
                      "rel": null
                  }
              },
              "created_at": "2015-10-12T20:26:13Z",
              "updated_at": "2015-10-12T20:26:13Z",
              "type": "question",
              "subject": "test",
              "raw_subject": "test",
              "description": "test 1234",
              "priority": "normal",
              "status": "open",
              "recipient": null,
              "requester_id": 1371651941,
              "submitter_id": 1371651941,
              "assignee_id": 1371651941,
              "organization_id": 157600651,
              "group_id": 24976891,
              "collaborator_ids": [],
              "forum_topic_id": null,
              "problem_id": null,
              "has_incidents": false,
              "due_at": null,
              "tags": [
                  "jpa"
              ],
              "custom_fields": [
                  {
                      "id": 28089621,
                      "value": "jpa"
                  },
                  {
                      "id": 28087191,
                      "value": null
                  },
                  {
                      "id": 28087201,
                      "value": null
                  }
              ],
              "satisfaction_rating": null,
              "sharing_agreement_ids": [],
              "fields": [
                  {
                      "id": 28089621,
                      "value": "jpa"
                  },
                  {
                      "id": 28087191,
                      "value": null
                  },
                  {
                      "id": 28087201,
                      "value": null
                  }
              ],
              "brand_id": 782281
          },
          {
              "url": "https://xxx.zendesk.com/api/v2/tickets/3.json",
              "id": 3,
              "external_id": null,
              "via": {
                  "channel": "web",
                  "source": {
                      "from": {},
                      "to": {},
                      "rel": null
                  }
              },
              "created_at": "2015-10-13T12:22:39Z",
              "updated_at": "2015-10-13T12:22:39Z",
              "type": null,
              "subject": "Test 2",
              "raw_subject": "Test 2",
              "description": "test content",
              "priority": null,
              "status": "open",
              "recipient": null,
              "requester_id": 1371651941,
              "submitter_id": 1371651941,
              "assignee_id": 1371651941,
              "organization_id": 157600651,
              "group_id": 24976891,
              "collaborator_ids": [],
              "forum_topic_id": null,
              "problem_id": null,
              "has_incidents": false,
              "due_at": null,
              "tags": [],
              "custom_fields": [
                  {
                      "id": 28089621,
                      "value": ""
                  },
                  {
                      "id": 28087191,
                      "value": null
                  },
                  {
                      "id": 28087201,
                      "value": null
                  }
              ],
              "satisfaction_rating": null,
              "sharing_agreement_ids": [],
              "fields": [
                  {
                      "id": 28089621,
                      "value": ""
                  },
                  {
                      "id": 28087191,
                      "value": null
                  },
                  {
                      "id": 28087201,
                      "value": null
                  }
              ],
              "brand_id": 782281
          }
      ],
      "next_page": null,
      "previous_page": null,
      "count": 2
  }
  </code></pre>
  
  <p>but, <strong>only if there is NO double Quotation mark in the description</strong> (and i think in other fields too). Seems that Zendesk API does no escaping for the Quotation Marks !?!</p>
  
  <p>But anyway i have valid JSON now, how can i load the JSON string in the Ticket object?</p>