-2

Here is the code i am getting error at last line where i am deserializing json to C# objects. Can you tell how to fix this.

   WebRequest req = WebRequest.Create(@"https://aaaaa.service-now.com/api/now/table/incident?sysparm_query=sys_updated_onBETWEENjavascript:gs.dateGenerate('2015-01-01','00:00:00')@javascript:gs.dateGenerate('2016-06-30','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); 
  • 4
    check your JSON and fix it – scrappedcola Sep 15 '16 at 17:26
  • It's giving you an error about the JSON you're trying to deserialize. The JSON has an extra trailing comma in an array. I know that because you told me. And you want somebody to fix this for you... how? By calling the serializer constructor differently? Could you, at the very, absolute least, share the text of the JSON itself? – 15ee8f99-57ff-4f92-890c-b56153 Sep 15 '16 at 17:26
  • ok thank you, the error is clear , i know that , but i get 50,000 records and i dont know how to find that particular record and each json array having 80 fields. I am just seeking how to get rid of that "," in json response thorugh code. – chaitanya dasari Sep 15 '16 at 17:31
  • I am not able to paste josn because it is too long to paste here for even records – chaitanya dasari Sep 15 '16 at 17:32

2 Answers2

0

Obviously your JSON is broken because it has a trailing comma in an array. If this JSON is coming from someplace you don't control, you should tell them to fix their broken implementation, or you should use a more tolerant JSON deserializer (I don't have one to recommend, unfortunately).

However, in the meantime you can work around the problem, if we make some assumptions. The biggest assumption is that you don't have any quoted fields which contain ",]" or ",}". If this assumption is accurate, then you can use a simple regex to find places where there are commas at the end of arrays or objects and remove them:

 //using System.Text.RegularExpressions
 var rx = new Regex(@",(\s*[\]}])", RegexOptions.Multiline);
 responseValue = rx.Replace(responseValue, "$1");

If this is your own JSON that you can edit yourself, then just open Notepad++ or some other text editor capable of searching with regex and search for ",\s*[\]}]" (with ". matches newline" turned on or equivalent) to find your offending comma.

Kevin Fee
  • 495
  • 4
  • 17
  • Thank you Kevin, yes JSON is coming from someplace where i cannot control, I will surely try to use Regex and see how it goes. But my json looks like this { "result": [ { "parent": "", "made_sla": "true", "caused_by": "", "watch_list": "", "upon_reject": "cancel", "sys_updated_on": "2016-06-27 14:47:52", "approval_history": "", "number": "INC0017004", "resolved_by": "" } ] } – chaitanya dasari Sep 15 '16 at 18:15
0

I had this exception and for me, I found there was comma character at the end. Simple List example:

  [
    { "id": "1234" },
    { "id": "5678" },
  ]

The last element in the list has a trailing ','. I deleted this and deserialization worked fine.

JohnG79
  • 1,485
  • 1
  • 17
  • 23