2

I am getting a JSON response from third party which I cannot control. Sometime this response throws illegal characters at the end of the string. Here is an example of correct one.

{
    "result": [
        {
            "parent": "",
            "made_sla": "true",
            "caused_by": "",
            "watch_list": "",
            "upon_reject": "cancel",
            "sys_updated_on": "2016-09-13 19:00:01",
            "approval_history": "",
            "category": "SPIN Station"
        }
    ]
}

An example of wrong string. Here you can see at last it throwing extra comma instead of closing.

{
    "result": [
        {
            "parent": "",
            "made_sla": "true",
            "caused_by": "",
            "watch_list": "",
            "upon_reject": "cancel",
            "sys_updated_on": "2016-09-13 19:00:01",
            "approval_history": "",
            "category": "SPIN Station"
        }
    ],

Here is my code in c#. Can anyone tell how to replace the illegal comma with closing curly bracket in the response value string in the code below before deserializing

 WebRequest req = WebRequest.Create(@"https://aaaa.service-now.com/api/now/table/incident?sysparm_query=sys_updated_onBETWEENjavascript:gs.dateGenerate('2016-09-10','00:00:00')@javascript:gs.dateGenerate('2016-09-13','23:59:59')");
     req.Method = "GET";
     req.Headers["Authorization"] = "Basic " + Convert.ToBase64String(Encoding.Default.GetBytes("username:Password"));
     req.ContentType = "application/xml";

     HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
     var responseValue = string.Empty;
     using (var responseStream = resp.GetResponseStream())
     {

          if (responseStream != null)
              using (var reader = new StreamReader(responseStream))
                  responseValue = reader.ReadToEnd();
     }

     JavaScriptSerializer ser = new JavaScriptSerializer();
     ser.MaxJsonLength = 2147483647;
     ser.RegisterConverters(new List<JavaScriptConverter> { new ResultConverter() });
     RootObject ro = ser.Deserialize<RootObject>(responseValue);
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
  • 1
    Step 1 is to push back hard against the company that's giving you bad data, to fix their stuff. If they're not building proper JSON, then there's a good chance that they're building the string manually, so it's very possibly they'll also screw up other JSON rules, which you can't predict or work around. – Joe Enos Sep 16 '16 at 19:17
  • 3
    Are you sure you arent getting a partial msg? – Ňɏssa Pøngjǣrdenlarp Sep 16 '16 at 19:25
  • But that error comes randomly,sometimes it throws and sometimes doesnot, i think its something with c# than the company sending. We get 50,000 records and 85 fields for each record. – chaitanya dasari Sep 16 '16 at 20:12
  • I am sure its not partial message – chaitanya dasari Sep 16 '16 at 20:12
  • 2
    Looks like truncation is happening at some point in the process. As far as auto-correcting bad JSON, that sounds like a nightmare that you are never going to win. – btberry Sep 16 '16 at 20:15
  • Why do you set your content-type to "application/xml" ? – makim Sep 26 '16 at 10:03
  • This error is resolved when we increased load time of json response in servicenow configuration – chaitanya dasari Nov 03 '16 at 17:51

1 Answers1

0

I see you are connecting to ServiceNow. That is odd if you are receiving a malformed JSON string - I would raise a ticket with hi.service-now.com

If you do need to correct your side then manipulate your responseValue before deserialising.

 if (responseValue.Substring(responseValue.Length-2, 2)=="],") {
 responseValue = responseValue.Remove(str.Length -1, 1) + "}";
}

p.s. I've not tested - and you would need to take a close look at the input JSON string to ensure there are no tailing spaces.

Gavin
  • 25
  • 4