-1

I have the following JSON(modified) which is read from a HttpWebRequest response stream, and I want to extract the value of "sys_id":

{
  "result": {
    "number": "INC0012618",
    "opened_by": {
      "link": "//XXX.service-now.com/api/now/v1/table/sys_user/38723e5ddb42f200deb8f6fcbf96196d",
      "value": "38723e5ddb42f200deb8f6fcbf96196d"
    },
    "sys_created_on": "2017-07-20 14:41:52",
    "sys_domain": {
      "link": "://XXX.service-now.com/api/now/v1/table/sys_user_group/global",
      "value": "global"
    },
    "u_incident_summary": "",
    "business_service": {
      "link": "://xxx.service-now.com/api/now/v1/table/cmdb_ci_service/IT services",
      "value": "IT services"
    },
    "work_end": "",
    "caller_id": {
      "link": "://xxxx.service-now.com/api/now/v1/table/sys_user/71f5455d4f735e000d7f0ed11310c719",
      "value": "71f5455d4f735e000d7f0ed11310c719"
    },
    "close_code": "",
    "assignment_group": {
      "link": "://xxx.service-now.com/api/now/v1/table/sys_user_group/9e158987dba27a007ea0f4e9bf961983",
      "value": "9e158987dba27a007ea0f4e9bf961983"
    },
    "sys_id": "7fb4e50edb8c0b007ea0f4e9bf9619ba",
    "u_outage_start_time": ""
  }
}

This is what I have tried so far, where incident_responce is a string containing the JSON above:

var jObject = JObject.Parse(incident_responce); 
var value = (string)jObject["sys_id"]; 
Console.WriteLine(value);

But, it didn't work, I think because there is "result" at the start. How can I retrieve this value?

dbc
  • 104,963
  • 20
  • 228
  • 340
  • used this but it didnt worked because i think as there is result in starting { string incident_responce = streamReader.ReadToEnd(); var jObject = JObject.Parse(incident_responce); var value = (string)jObject["sys_id"]; Console.WriteLine(value);} please suggest. – urvashi rao Jul 20 '17 at 14:00
  • The presence of backslashes there indicates that this is not JSON. You will have to use those backslashes in your code, but the output should look like `{"result":{ ... }}` without the backslashes. Are you getting this text from an API? – halfer Jul 20 '17 at 14:08

1 Answers1

0

As you suspected, your initial attempt fails because "sys_id" is nested inside the "result" object:

{ "result": { ... "sys_id":"7fb4e50edb8c0b007ea0f4e9bf9619ba" } }

It's easier to see this if you indent and format your JSON, for instance by uploading it to https://jsonformatter.curiousconcept.com/.

Such a nested value can be queried directly by using JToken.SelectToken():

var root = JToken.Parse(incident_responce); 
var value = (string)root.SelectToken("result.sys_id");          

SelectToken() supports querying for values deep within the JSON container hierarchy using JSONPath syntax. If the "result.sys_id" token is not found, null is returned.

Alternatively, you could use Json.NET's support for querying JSON hierarchies using dynamic functionality:

dynamic root = JToken.Parse(incident_responce); 
var value = (string)root.result.sys_id;

However, if the "result" token is not found, a RuntimeBinderException will be thrown instead of a null value returned.

Working .Net fiddle.

dbc
  • 104,963
  • 20
  • 228
  • 340