-1

I have this JSON that I get from API, I have trouble with Parsing the JSON to get the value I want this is the JSON

[\"{\\\"UPDATE_TYPE\\\":\\\"NOTE_ADDED\\\",\\\"UPDATE_TEXT\\\":\\\"Notenya begini\\\",\\\"UPDATE_TIME\\\":\\\"Fri, Dec 4, 17:02\\\",\\\"UPDATE_TIME_EPOCH\\\":1607076127834,\\\"ATTACHMENT\\\":\\\"\\\"}\",\"{\\\"UPDATE_TYPE\\\":\\\"STATUS_UPDATED\\\",\\\"UPDATE_TEXT\\\":\\\"Complete\\\",\\\"UPDATE_TIME\\\":\\\"Fri, Dec 4, 17:02\\\",\\\"UPDATE_TIME_EPOCH\\\":1607076127840,\\\"ATTACHMENT\\\":\\\"\\\"}\"]

I already tried to Deserialize it twice to remove the encoding. And I try to use Regex too but I still get an error when tried to parse using Newtonsoft JAray.Parse. I never encounter JSON like this before. any suggestion to remove the encoding and parse the value from that JSON?

Theodorus Agum Gumilang
  • 1,414
  • 4
  • 23
  • 46
  • 2
    This has been double escaped. Assuming this is the ***actual*** data you are getting, wherever you are getting it from is doing something very wrong – TheGeneral Dec 04 '20 at 11:04
  • if you delete the doubled backslashes with a `.Replace(@"\", @"");` you should be able to deserialize – CodeMonkey1770 Dec 04 '20 at 11:18
  • " I never encounter JSON like this before"...it's not valid JSON, that's why. Whatever generated it needs fixing, rather than you finding workarounds. – ADyson Dec 04 '20 at 11:31
  • that's right... what probably happened is that it got somehow escaped twice. 1st step " to \", 2nd step \" to \\\" – CodeMonkey1770 Dec 04 '20 at 11:34
  • Ok, thanks for clarifying this, I already spend a couple of hours to parse this JSON. Since there is nothing I can do for now. I will just report this as a bug to the service provider that populate this JSON. But I hope there is still a workaround because it maybe takes time to change it because the JSON I consume is from a kinda big Application. – Theodorus Agum Gumilang Dec 04 '20 at 11:43
  • Well you can do what CodeMonkey suggested as a workaround - remove the slashes before trying to deserialise. Of course the downside is that this can also potentially remove valid slashes which are genuinely part of the text values in the JSON. – ADyson Dec 04 '20 at 11:44
  • 2
    Are you looking at this in a debugger? – Brian Rogers Dec 05 '20 at 19:42

1 Answers1

1
  1. Remove double quotes between [ and {, same at the end of the string.
  2. Remove the \ in json string.

This is the specific step, intercepted part of the json string.

        var str = "[\"{\\\"UPDATE_TYPE\\\":\\\"NOTE_ADDED\\\",\\\"UPDATE_TEXT\\\":\\\"Notenya begini\\\",\\\"UPDATE_TIME\\\":\\\"Fri, Dec 4, 17:02\\\"}\"]";

        Regex rg = new Regex("(?<=(" + "{" + "))[.\\s\\S]*?(?=(" + "}" + "))", RegexOptions.Multiline | RegexOptions.Singleline);
        var lstr= rg.Match(str).Value;
        
        var jstr = "[{" + lstr + "}]";
        var model=jstr.Replace(@"\", "");
        var result=JsonConvert.DeserializeObject<List<Model>>(model);

Model.

public class Model
{
    public string UPDATE_TYPE { get; set; }
    public string UPDATE_TEXT { get; set; }
    public string UPDATE_TIME { get; set; }
}

enter image description here

Karney.
  • 4,803
  • 2
  • 7
  • 11