skip to Main Content

I’m am trying to parse a JSON string with multiple results. I am calling an API from our help desk ticketing system. I am able to get all the ticketing information from the API that I want into a var but I am unable to parse it into individual strings.

This is what I am currently doing using JSON Newtonsoft.

var ticketData = JsonConvert.DeserializeObject <TicketData> (tickets);

tickets is the JSON string. It has the correct data in it but when I try to output one of the values

Console.WriteLine(ticketData.summary.ToString());

It doesn’t work and displays 0 or null value. I am able to get this to work in another JSON result but it only has one result in it.

Here are my classes defining the data stored in this JSON:

public class TicketData
{
    public List<Data> datas { get; set; }
}

public class Data
{
    public string assignedAppUser { get; set; }
    public decimal createTime { get; set; }
    public string description { get; set; }
    public int id { get; set; }
    public string priority { get; set; }
    public string requestor { get; set; }
    public string severity { get; set; }
    public decimal solvedTime { get; set; }
    public string source { get; set; }
    public List<Status> statuses { get; set; }
    public string summary { get; set; }
    public string tags { get; set; }
    public string ticketForm { get; set; }
    public string triggeredCondition { get; set; }
    public string type { get; set; }
}

public class Status
{
    public int statusID { get; set; }
    public string displayName { get; set; }
    public int parentID { get; set; }
}

Here is an example of the JSON that is being returned by the API:

{
   "data":[
      {
         "assignedAppUser":"Nathan",
         "createTime":1674593050.296000000,
         "description":"Hi HD,nnI think the Cert Emails need a check up. I don't think that all of them are being generated and going out.",
         "id":1006,
         "priority":"MEDIUM",
         "requester":"Lisa",
         "severity":"MODERATE",
         "solvedTime":1675722696.475000000,
         "source":"EMAIL",
         "status":{
            "statusId":6000,
            "displayName":"Closed",
            "parentId":6000
         },
         "summary":"Lisa Holden / Troubleshoot Missing Cert Renewal Emails",
         "tags":[
            "Programming",
            "Application"
         ],
         "ticketForm":"Default",
         "triggeredCondition":null,
         "type":"PROBLEM"
      },
      {
         "assignedAppUser":"Nathan",
         "createTime":1674593291.600000000,
         "description":"Identify best solution for monitoring battery backup UPS devices in all areas so we can be notified anytime they switch to battery power even if endpoint devices don't go down.",
         "id":1007,
         "priority":"NONE",
         "requester":"Nathan",
         "severity":"NONE",
         "solvedTime":null,
         "source":"TECHNICIAN",
         "status":{
            "statusId":6000,
            "displayName":"Closed",
            "parentId":6000
         },
         "summary":"Configure monitoring on battery UPSs",
         "tags":[
            "Hardware"
         ],
         "ticketForm":"Default",
         "triggeredCondition":null,
         "type":null
      }
   ],
   "metadata":{
      "columns":[
         "createTime",
         "requester",
         "status",
         "type",
         "summary",
         "assignedAppUser",
         "source",
         "id",
         "solvedTime"
      ],
      "sortBy":[
         {
            "field":"id",
            "direction":"ASC"
         }
      ],
      "attributes":{
         
      },
      "filters":null,
      "lastCursorId":2,
      "allColumns":[
         "createTime",
         "requester",
         "status",
         "type",
         "summary",
         "assignedAppUser",
         "source",
         "id",
         "solvedTime"
      ],
      "columnNamesForExporting":[
         "createTime",
         "requester",
         "status",
         "type",
         "summary",
         "assignedAppUser",
         "source",
         "id",
         "solvedTime"
      ],
      "allRequiredColumns":[
         "createTime",
         "requester",
         "status",
         "type",
         "summary",
         "assignedAppUser",
         "source",
         "id",
         "solvedTime",
         "ticketForm",
         "description",
         "priority",
         "severity",
         "tags",
         "triggeredCondition"
      ]
   }
}

Any help would be appreciated. Thank you.

2

Answers


  1. The JSON structure you’re dealing with has a root element named "data" containing an array of ticket information. Your TicketData class reflects this structure with a list of Data objects. Therefore, you should deserialize it accordingly.

    Your deserialization code should look like this:

    var ticketData = JsonConvert.DeserializeObject<TicketData>(tickets);
        
        foreach (var ticket in ticketData.datas)
        {
            Console.WriteLine(ticket.summary);
            // Access other properties as needed
        }
    

    Note that datas is the property name in your TicketData class that corresponds to the "data" array in the JSON. Iterate through this list to access each individual ticket’s properties.

    Login or Signup to reply.
  2. You need to create objects like this to map with the json content

    public class Root
    {
        public List<Data> data { get; set; }
        public Metadata metadata { get; set; }
    }
    
     public class Metadata
    {
        public List<string> columns { get; set; }
        public List<SortBy> sortBy { get; set; }
        public Dictionary<string, object> attributes { get; set; }
        public object filters { get; set; }
        public int? lastCursorId { get; set; }
        public List<string> allColumns { get; set; }
        public List<string> columnNamesForExporting { get; set; }
        public List<string> allRequiredColumns { get; set; }
    }
    
    public class Data
    {
        public string assignedAppUser { get; set; }
        public double createTime { get; set; }
        public string description { get; set; }
        public int id { get; set; }
        public string priority { get; set; }
        public string requester { get; set; }
        public string severity { get; set; }
        public double? solvedTime { get; set; }
        public string source { get; set; }
        public Status status { get; set; }
        public string summary { get; set; }
        public List<string> tags { get; set; }
        public string ticketForm { get; set; }
        public object triggeredCondition { get; set; }
        public string type { get; set; }
    }
    
    public class SortBy
    {
        public string field { get; set; }
        public string direction { get; set; }
    }
    
    public class Status
    {
        public int statusID { get; set; }
        public string displayName { get; set; }
        public int parentID { get; set; }
    }
    

    And then "data" is an element of the object so you can access it like this

    you can get like this or get from file like below

    var ticketData = JsonConvert.DeserializeObject<Root>("replace json here");
    

    enter image description here

    var ticketData = JsonConvert.DeserializeObject<Root>(File.ReadAllText("replace json file here"));
    var firstSummary = ticketData.data[0].summary;
    Console.WriteLine(firstSummary);
    

    replace your Json input with the file here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search