1

I am calling the ServiceNow Incidents table and pulling back one incident like this. https://mydevInstance.service-now.com/api/now/v1/table/incident?sysparm_limit=1

var client = new RestClient("https://mydevInstance.service-now.com/api/now/v1/table/incident?sysparm_limit=1");
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", "Basic myAuthKey");
IRestResponse response = client.Execute(request);

The JSON it returns in RESTSharp looks like this.

{
    "result": [
        {
            "parent": "",
            "made_sla": "true",
            "caused_by": "",
            "watch_list": "",
            "upon_reject": "cancel",
            "resolved_by": {
                "link": "https://mydevInstance.service-now.com/api/now/v1/table/sys_user/5137153cc611227c000bbd1bd8cd2007",
                "value": "5137153cc611227c000bbd1bd8cd2007"
            },
            "approval_history": "",
            "number": "INC0000060"
        }
    ]
}

How do I create a C# list or array of all the Keys under result? I can't Serialize the object with JSON.Net because additional keys can be added over time.

Alenros
  • 816
  • 7
  • 23
TMAC 23
  • 93
  • 6
  • Depending on what version of Visual Studio you are using, you should have a "Paste" option that allows you to 'paste' some JSON (or XML) as a C# class. This feature may also be under "Paste Special" ... check your "Edit" menu – Glenn Ferrie Dec 21 '20 at 18:15
  • That doesn't work in this instance as the class would change when additional columns are added to the incident, which results in additional keys added to the JSON. I need the list of keys. – TMAC 23 Dec 21 '20 at 18:33
  • When the table in service now changes, you will need to make appropriate changes to the class. – Jawad Dec 21 '20 at 19:25
  • I don't want/need a class. I just need a list, string, or array of all the keys from JSON. – TMAC 23 Dec 21 '20 at 19:43

3 Answers3

1

You need to grab the sample of the JSON content, then make a C# class using the 'Paste Special' option I described.

menu option

Then you can use the JsonConvert.DeserializeObject<T> (in a nuget package by Newtonsoft) to deserialize your web service response in a C# object instance.

Here are the C# classes I generated with your JSON object unaltered:

public class Rootobject
{
    public Result[] result { get; set; }
}

public class Result
{ 
    public string parent { get; set; }
    public string made_sla { get; set; }
    public string caused_by { get; set; }
    public string watch_list { get; set; }
    public string upon_reject { get; set; }
    public Resolved_By resolved_by { get; set; }
    public string approval_history { get; set; }
    public string number { get; set; }
}

public class Resolved_By
{
    public string link { get; set; }
    public string value { get; set; }
}

You use this type like this:

var json = "t-b-d"; // From Web Service call
Rootobject response = JsonConvert.DeserializeObject<Rootobject>(json);
// use the response object.

** UPDATED **

If you need a more flexible model, all JSON will deserialize into Dictionary<string, string>, but I have found that serialization / deserialization results are more reliable when the model is consistent

var response = JsonConvert.DeserializeObject<Dictionary<string,string>>(json);
Glenn Ferrie
  • 10,290
  • 3
  • 42
  • 73
  • That doesn't work in this instance as the class would change when additional columns are added to the incident, which results in additional keys added to the JSON. I need the list of keys. – TMAC 23 Dec 21 '20 at 18:57
  • List of keys are going to depend on your service now table. Normally I go to the site in Chrome to get all fields and create a class based on that – Jawad Dec 21 '20 at 19:22
  • The list is going to be different for each company that uses it. I need the list of keys programmatically. – TMAC 23 Dec 21 '20 at 19:47
  • @Glenn still didn't work as it throws an error Unexpected character encountered while parsing value: [. Path 'result', line 1, position 11. – TMAC 23 Dec 23 '20 at 18:39
1

Here is what does work using System.Text.Json

var incidentFields = new List<string>();

var doc = JsonDocument.Parse(json);
foreach (var o in doc.RootElement.GetProperty("result").EnumerateArray())
{
    foreach (var p in o.EnumerateObject())
    {
        incidentFields.Add(p.Name.ToString());
    }
}
TMAC 23
  • 93
  • 6
0

I created a library that handles that by default. (You can add custom types also)

https://autodati.github.io/ServiceNow.Core/

Emerson Bottero
  • 328
  • 5
  • 9