Try this:

"maingame": {
  "day1": {
    "text1": "Tag 1",
     "text2": "Heute startet unsere Rundreise \" Example text\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.</strong> "
  }
}

(just one backslash (\) in front of quotes).

Answer from kamituel on Stack Overflow
Top answer
1 of 11
774

Try this:

"maingame": {
  "day1": {
    "text1": "Tag 1",
     "text2": "Heute startet unsere Rundreise \" Example text\". Jeden Tag wird ein neues Reiseziel angesteuert bis wir.</strong> "
  }
}

(just one backslash (\) in front of quotes).

2 of 11
75

When and where to use \\\" instead. OK if you are like me you will feel just as silly as I did when I realized what I was doing after I found this thread.

If you're making a .json text file/stream and importing the data from there then the main stream answer of just one backslash before the double quotes:\" is the one you're looking for.

However if you're like me and you're trying to get the w3schools.com "Tryit Editor" to have a double quotes in the output of the JSON.parse(text), then the one you're looking for is the triple backslash double quotes \\\". This is because you're building your text string within an HTML <script> block, and the first double backslash inserts a single backslash into the string variable then the following backslash double quote inserts the double quote into the string so that the resulting script string contains the \" from the standard answer and the JSON parser will parse this as just the double quotes.

<script>
  var text="{";
  text += '"quip":"\\\"If nobody is listening, then you\'re likely talking to the wrong audience.\\\""';
  text += "}";
  var obj=JSON.parse(text);
</script>

+1: since it's a JavaScript text string, a double backslash double quote \\" would work too; because the double quote does not need escaped within a single quoted string eg '\"' and '"' result in the same JS string.

🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-escape-double-quotes-in-json
How to Escape Double Quotes in JSON? - GeeksforGeeks
September 24, 2024 - Most programming languages provide built-in libraries or functions to encode JSON objects properly, automatically escaping necessary characters like double quotes. In JavaScript, we can use the JSON.stringify() function, which safely converts ...
Discussions

[SOLVED] How to escape double quotes from jsonpath string?
All, I would like to extract OK from the following response: {“status”:“OK”,“statustext”:“Switching the screen off”} But I fail. I found a very helpful site to check some approaches, but did not succeed. https://jso… More on community.openhab.org
🌐 community.openhab.org
1
0
October 3, 2017
What's the reason "JSON.stringify" add escape character \ when it's json object, but not if it's json array?
looks like you stringify a string and not and actual object. do this: JSON.stringify(obj); instead of this: JSON.stringify(json_obj); More on reddit.com
🌐 r/node
6
0
April 17, 2022
How do I ensure that JSON mode properly escapes quotation marks?
When requesting json_object completions, I am encountering a problem where the generated text cuts off randomly mid-sentence. When this happens, the completion reason is stop, so this is not a case of exhausting allowed completion tokens. The pattern I’ve noticed is that this consistently ... More on community.openai.com
🌐 community.openai.com
1
1
February 8, 2024
Replacing/Escaping double quotes
I’m trying to pass Json with an ahref/URL in a string, but the Double quotes are presenting a problem. I thought I could use something as simple as this but obviously the quotes with inside the function don’t work. rep… More on community.make.com
🌐 community.make.com
1
0
May 21, 2024
🌐
Quora
quora.com › How-do-I-escape-double-quotes-in-a-JSON-value
How to escape double quotes in a JSON value - Quora
Seems cleaner to me to use the same standard throughout. ... In JSON, double quotes (") delimit strings, so any literal double quote character inside a string must be escaped with a backslash ().
Top answer
1 of 2
1
Okay then I think there's something wrong with your API.I think what is going on is that your API:Accepts JSON correctly.Stores the object correctly.Returns the JSON wrong.I have the feeling your API might accept application/json but returns application/text or something like that, can you check?Basically what should happen when you create a resource is that your API should return the same thing you sent to it, plus the ID of the created resource. For example:Create a resourcePOST /address{"street": "this is a \" test"}>>> {"id": "1", "street": "this is a \" test"}Fetch a resourceGET /address/1>>> {"id": "1", "street": "this is a \" test"}Database contentid street----- ----------------1 this is a " test
2 of 2
2
Hi Milan, Unfortunately, trying to correct the JSON string before parsing is going to be incredibly complicated. I'm saying this because the field in question (street) seems to be free-text, so it will be basically impossible to identify what is JSON syntax and what is field content.Imagine I entered the following value for the street field:Fake Street", "fakeProperty": "fakeThis would result in the following JSON (expected JSON syntax in blue, value in underlined red)"street: "Fake Street", "fakeProperty": "fake"As you can see, this is actually valid JSON! I don't know where your data comes from, but if it comes from an input form for example, then anybody could manipulate your JSON (and resulting JS objects) by entering such values in the input form fields, and you would never be able to detect it as "invalid" (at least not in a generic way). For that reason I don't think that finding a JS hack to solve to fix the JSON string is the right way to solve the issue. Instead, i think you should attempt to fix the method of fetching the JSON so that it maintains its correctly escaped format. Could you tell us how you're doing that?If that is not possible for whatever reason, then try to avoid double-quotes in the data by adding stricter validation on input and doing a one-shot fix for all existing data.
🌐
JSON Formatter
jsonformatter.org › json-escape
Best JSON Escape Characters, Double Quotes and Backslash tool
Backspace is replaced with \b, Form feed is replaced with \f, Newline is replaced with \n, Carriage return is replaced with \r, Tab is replaced with \t, Double quote is replaced with \", Backslash is replaced with \\. JSON Escape is an essential ...
🌐
openHAB Community
community.openhab.org › setup, configuration and use › scripts & rules
[SOLVED] How to escape double quotes from jsonpath string? - Scripts & Rules - openHAB Community
October 3, 2017 - All, I would like to extract OK from the following response: {“status”:“OK”,“statustext”:“Switching the screen off”} But I fail. I found a very helpful site to check some approaches, but did not succeed. https://jsonpath.curiousconcept.com/ With val String response = transform("JSONPATH", "$.status", json) the website above returns “OK”. Actually I have two issues: I would like to get OK instead of “OK” When I run the website from a browser, I get the response mentioned above: {...
🌐
codestudy
codestudy.net › blog › how-to-escape-double-quotes-in-json
How to Escape Double Quotes in JSON: Fix Backslashes Showing in HTML Rendering — codestudy.net
To include a double quote inside a JSON string, prefix it with \: ... Now the inner " is escaped as \", and JSON parses the string correctly. Manual escaping works for simple cases, but for dynamic data, always use built-in JSON libraries to ...
Find elsewhere
🌐
FreeFormatter
freeformatter.com › json-escape.html
Free Online JSON Escape / Unescape Tool - FreeFormatter.com
The following characters are reserved in JSON and must be properly escaped to be used in strings: Backspace is replaced with \b · Form feed is replaced with \f · Newline is replaced with \n · Carriage return is replaced with \r · Tab is replaced with \t · Double quote is replaced with \" ...
🌐
ServiceNow Community
servicenow.com › community › developer-forum › how-to-escape-double-quotes-in-a-json › m-p › 1412266
Solved: How to escape double quotes in a JSON? - ServiceNow Community
February 16, 2022 - Hi community, I have the following field where the user can insert any character. For example: Then, behind the scenes once the form is submitted, a script include makes a REST API call. However, the double quotes that the user inserted make the integration fail.
🌐
Microsoft Power Platform Community
powerusers.microsoft.com › t5 › Building-Flows › Escaped-double-quotes-and-Parse-JSON › td-p › 938273
https://powerusers.microsoft.com/t5/Building-Flows...
April 6, 2024 - Quickly search for answers, join discussions, post questions, and work smarter in your business applications by joining the Microsoft Dynamics 365 Community.
🌐
Javascript-coder
javascript-coder.com › tricks › javascript-escape-double-quotes-in-string
How to escape double qoutes in a string | JavaScript Coder
Inside a template literal, you don’t have to escape single or double-quotes. const templateLiteral=`"I'm OK with this Agreement" Said he. "Me too!". I said.` ... Suppose you received a string that may contain quotes and other special characters. You can use JSON.stringify() to escape the special characters like so:
🌐
SSOJet
ssojet.com › escaping › json-escaping-in-nodejs
JSON Escaping in NodeJS | Escaping Techniques in Programming
Node.js provides the JSON.stringify() method, the go-to for converting JavaScript objects into valid JSON strings. This built-in function expertly manages the necessary escaping for characters that have special meaning within JSON, such as double ...
🌐
Reddit
reddit.com › r/node › what's the reason "json.stringify" add escape character \ when it's json object, but not if it's json array?
r/node on Reddit: What's the reason "JSON.stringify" add escape character \ when it's json object, but not if it's json array?
April 17, 2022 -

****************Here is the result **************

"{\"name\": \"Peter\", \"age\": 22, \"country\": \"beast of no nation\"}"

****************Here is the code**************

var json_obj = '{"name": "Peter", "age": 22, "country": "beast of no nation"}';

// deserialization: Converting JSON-encoded string to JS object

var obj = JSON.parse(json_obj);

console.log(obj);

/*

console.log(obj.name);

console.log(obj.age);

console.log(obj.country);

*/

// serialization: JS object back to JSON-encoded string

var json_obj2 = JSON.stringify(json_obj);

console.log(json_obj2);

console.log("*************************************************************************");

//var json_array = '["Peter", "Lisa", "Tom"]';

var json_array = '[{"name": "Peter", "age": 22, "country": "beast of no nation"},{"name": "Lisa", "age": 22, "country": "beast of no nation"}]';

// deserialization: Converting JSON-encoded string to JS object

var array = JSON.parse(json_array);

console.log(array);

// serialization: JS object back to JSON-encoded string

var json_array2 = JSON.stringify(array);

console.log(json_array2);

🌐
Keploy
keploy.io › home › community › json escape and unescape
JSON Escape and Unescape | Keploy Blog
June 30, 2024 - A beginner's guide to escaping and unescaping JSON strings. Learn when and why to use these methods with simple examples.
Top answer
1 of 3
6

You say the server is returning the JSON (omitting the enclosing quotes):

{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}

This is invalid JSON. The quote marks in JSON surrounding strings and property names should not be preceded by a backslash. The backslash in JSON is strictly for inserting double quote marks inside a string. (It can also be used to escape other characters inside strings, but that is not relevant here.)

Correct JSON would be:

{"data":[], "SkipToken":"", "top":""}

If your server returned this, it would parse correctly.

The confusion here, and the reports by other posters that it seems like your string should work, lies in the fact that in a simple-minded test, where I type this string into the console:

var x = "{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}";

the JavaScript string literal escaping mechanism, which is entirely distinct from the use of escapes in JSON, results in a string with the value

{"data":[], "SkipToken":"", "top":""}

which of course JSON.parse can handle just fine. But Javascript string escaping applies to string literals in source code, not to things coming down from the server.

To fix the server's incorrectly-escaped JSON, you have two possibilities. One is to tell the server guys they don't need to (and must not) put backslashes before quote marks (except for quote marks inside strings). Then everything will work.

The other approach is to undo the escaping yourself before handing it off to JSON.parse. A first cut at this would be a simple regexp such as

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

as in

var dataList = JSON.parse(data.replace(/\\"/g, '"')

It might need additional tweaking depending on how the server guys are escaping quotes inside strings; are they sending \"\\"\", or possibly \"\\\"\"?

I cannot explain why this code that was working suddenly stopped working. My best guess is a change on the server side that started escaping the double quotes.

2 of 3
2

Since there is nothing wrong with the JSON string you gave us, the only other explanation is that the data being passed to your function is something other than what you listed.

To test this hypothesis, run the following code:

dspservice.callService(URL, "GET", "", handler(data));

function handler(data) {
    var goodData = "{\"data\":[], \"SkipToken\":\"\", \"top\":\"\"}";
    alert(goodData);                         // display the correct JSON string
    var goodDataList = JSON.parse(goodData); // parse good string (should work)
    alert(data);                             // display string in question
    var dataList = JSON.parse(data);         // try to parse it (should fail)
}

If the goodData JSON string can be parsed with no issues, and data appears to be incorrectly-formatted, then you have the answer to your question.

Place a breakpoint on the first line of the handler function, where goodData is defined. Then step through the code. From what you told me in your comments, it is still crashing during a JSON parse, but I'm willing to wager that it is failing on the second parse and not the first.