0

I have a list in C#. In my page, I give this list with : '@Html.Raw(Json.Encode(Model.MyList))';

I stringify this object, the obj is good.

But when I try to parse it to an object JSON with 'JSON.parse', I have this exception 'Unexpected token , in JSON at position 26 SyntaxError: Unexpected token , in JSON at position 26 at JSON.parse ()'.

This is because of the double quotes in some keys of this object, like "Example4".

      "Example2":"C01150",
      "Example3":"C01150",
      "Example4":"vase pompe de flèche ATC T 16"","
      "Example5":false," 
     },

I have tried this solution : https://salesforce.stackexchange.com/questions/233068/replace-double-quotes-with-single-quote-in-a-json-string

And this one : How to escape double quotes in JSON

So, I do this : function jsonEscape(str) { return str.replace(/\n/g, "\\\\n").replace(/\r/g, "\\\\r").replace(/\t/g,"\\\\t").replace(/\\"/, '\\"');}

But the double quotes don't move...

JSON.parse result :

"Example4":"vase pompe de flèche ATC T 16"","

I am trying to get this code :

"Example4": "vase pompe de flèche ATC T 16\"",

I want a valid JSON object.

Can someone help me ?

Little_Dev
  • 107
  • 8
  • 1
    "I stringify this object, the obj is good." it's not, you haven't used a proper JSON function for that, which is why you get invalid JSON. No good JSON serializer would output that. And this is what you would get if you were using `Json.Encode` https://dotnetfiddle.net/lOXtSb – Charlieface Jul 11 '22 at 09:53
  • Yes, thank you for your reply. That's exactly my problem. `Json.Encode ` is not a good alternative. – Little_Dev Jul 11 '22 at 12:03
  • Like I said, `Json.Encode` is not producing that invalid JSON text. Either you are using something else, or something is corrupting it somehow. Without further context (like where you store and retrieve this JSON) cannot help further. – Charlieface Jul 11 '22 at 12:32
  • This invalid json is invalid from its source (database), nothing is added or changed. My duty is to be able, from my C# code, to render it on a web page with JavaScript and show data as it is. – Little_Dev Jul 11 '22 at 12:54
  • Then you should fix what is creating invalid JSON. Why do you show the code `@Html.Raw(Json.Encode(Model.MyList))` if you are not actually using that? – Charlieface Jul 11 '22 at 12:57
  • I wanted to do JSON.parse in order to have access to each key and each value and then put that in a Datatable. However, I get an exception at the JSON.parse level because of the double quotes. I tried to repair the JSON with escapements but without success. – Little_Dev Jul 11 '22 at 13:07

2 Answers2

0

The last bit of regex,

replace(/\\"/, '\\"')

is looking for the sequence \" not "

If it read,

replace(/"/g, '\"')

then it would look for every instance (globally) of " and replace it with an escaped version \" safe for the parser.

ugy
  • 609
  • 4
  • 6
  • Thank you for your reply. indeed it's a good correction, unfortunately it's not enough, because my object is still encoded. – Little_Dev Jul 11 '22 at 12:04
0

try this

var str = `{
      "Example2":"C01150",
      "Example3":"C01150",
      "Example4":"vase pompe de flèche ATC T 16"","
      "Example5":false
     }`;
var arr = [];
var arrStr = str.replace("{", "").replace("}", "").split("\n");
arrStr.forEach((item) => {
  arr.push(item.trim().split(":"));
});
str = "";
for (let index = 0; index < arr.length; index++) {
  if (arr[index][1] == undefined) continue;
  arr[index][1] = arr[index][1].replace('"",', "").replace(",", "");
  console.log(arr[index][1]);
  str = str + arr[index][0] + ":" + arr[index][1] + ",";
}

str = "{" + str.substring(0, str.length - 1) + "}";
console.log(JSON.parse(str));
Serge
  • 40,935
  • 4
  • 18
  • 45