Search code examples
c#jsonparsingjson.netrestsharp

Two almost identical functions, one works, the other does not?


Working on writing a C# app to talk to our ServiceNow API. For some reason I can successfully ignore the root token of the JSON file it returns for the user, but not for the Incident. JSON returns seem to be almost identical save for the fact that one is from the incident table and the other is from the sys_user table.

Tried a couple of different ways of using Newtonsoft in the process of getting to the ignoring of the root token. Which has led me to where this code is at now (albeit unrefined)

void incidentCreator(string incID)
{
    var restRequest = new RestRequest(_incTableLink + "/" + incID);
    restRequest.AddParameter("sysparm_display_value", "true");
    restRequest.AddParameter("sysparm_fields", "assigned_to,number,sys_updated_on,sys_id,comments_and_work_notes");
    string incidentJsonString = _restInteractions.querySnow(restRequest);
    Incident incident = null;
    try
    {
        incident = JObject.Parse(incidentJsonString).SelectToken("result[0]").ToObject<Incident>();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }
    _incidentList.Add(incident);
}

void userGetter(string userID)
{
    var restRequest = new RestRequest(_userTableLink);
    restRequest.AddParameter("sysparm_query", "sys_id=" + userID);
    string userJsonString = _restInteractions.querySnow(restRequest);
    User user = null;
    try
    {
        user = JObject.Parse(userJsonString).SelectToken("result[0]").ToObject<User>();
    }
    catch (Exception e)
    {
        Console.WriteLine(e);
    }

    //User user = JsonConvert.DeserializeObject<User>(userJsonString);
    _userList.Add(user);
}

JSON string for incident that doesn't work

"{
  \"result\": {
    \"number\": \"INC1215653\",
    \"sys_id\": \"52764be80007fb00903d51fea4ade50f\",
    \"comments_and_work_notes\": \"2019-08-01 10: 46: 19 - PERSON (Additional comments)\\nEmailed PERSON 
admin of the site to check permissions\\n",
    \"sys_updated_on\": \"2019-08-01 10: 46: 17\",
    \"assigned_to\": \"Employee\"
  }
}"  

JSON string for user which does work

"{
  \"result\": [
    {
      \"calendar_integration\": \"1\",
      \"last_position_update\": \"\",
      \"u_lmc_account_indicator\": \"\",
      \"u_supply_chain\": \"false\",
    }
  ]
}"

No matter what I do with the incidentCreator() JSON parsing, using jsonconvert or the implementation present in current shared code. I either get "Object reference not set to an instance of an object." or just an object with all it's variables being null.

Any ideas? I'm sorry for the stripped back user json, it is a huge string and i couldn't be bothered cleaning it of PII before posting.


Solution

  • The JPath expression in your incidentCreator is incorrect.

    It should be:-

    try { incident = JObject.Parse(incidentJsonString).SelectToken("result").ToObject<Incident>(); }
    

    Also, It's possibly just a result of pasting the JSON into the question, but the "comments_and_work_notes" field of the incident JSON example isn't escaped properly; just check you're receiving valid JSON using a linter to be sure.