json parse error with double quotes
Well, finally, JSON's parse uses the same eval, so there's no difference when you give them smth. with incorrect syntax. In this case you have to escape correctly your quotes in php, and then escape them and their escaping slashes with json_encode
<?php
$json = '{"result": ["lunch", "\"Show\""] }';
echo json_encode($json);
?>
OUTPUT: "{\"result\": [\"lunch\", \"\\\"Show\\\"\"] }"
This should work on client-side JS (if I've made no typos).
JSON.parse error is giving error with double quotes string
The problem is the type of quote used in your JSON string, not the outer quotes. The JSON specification only allows for double quoted strings. You could use either type of quote to actually pass your JSON string to the parse()
function, though.
From the JSON spec:
JSON parse double quotes
JSON.parse('["\"d\""]')
\
is a special character in JS strings, so to include it in your JSON you need to escape it.
const yourString = '["\"d\""]';
const validJSON = '["\\"d\\""]'
const parsedData = JSON.parse(validJSON);
document.querySelector('#yourString').value = yourString;
document.querySelector('#validJSON').value = validJSON;
<p>Your string: <output id="yourString"></output>
<p>Valid JSON: <output id="validJSON"></output>
JSON.parse not working with double quotes in value
You have to escape the backslashes and quotation marks when you put the string representation of the object in a string using quotation mark as delimiter:
JSON.parse("{\"hi\":\"\\\"bye\\\"\"}")
When you use apostrophes as string delimiter you don't have to escape quotation marks (but you would have to escape apostrophes if you had any):
JSON.parse('{"hi":"\\"bye\\""}')
Error parsing JSON with escaped quotes inside single quotes
The unfortunate thing about this situation is that in the JavaScript code there is no difference between
var jsonData = '{"Key":"query","Value":"dept=\"Human Resources*\"","ValueType":"Edm.String"}'
and
var jsonData = '{"Key":"query","Value":"dept="Human Resources*"","ValueType":"Edm.String"}'
.
You could hardcode information you have about the JSON into the way you program it. For example, you could replace occurences of the regex ([\[\{,:]\s+)\"
by $1\"
but this would fail to work if the string Human Resources*
could also end in a :
, {
or ,
. This would also potentially cause security issues.
In my opinion, the best way to solve your problem would be to put the json response in a json file somewhere so that it can be read into a string by the javascript code that needs to use it.
Related Topics
How to Install the Intl Extension for Twig
Understanding PHP Curl_Multi_Exec
PHP - Count Specific Array Values
PHP - How to Implement Password Reset and Token Expiry
Eloquent Parent-Child Relationship on Same Model
PHP Artisan Migrate Throwing [Pdo Exception] Could Not Find Driver - Using Laravel
PHP How to Get the Base Domain/Url
Symfony\Component\Httpkernel\Exception\Notfoundhttpexception Laravel
Symfony2: How to Access Service from Template
PHP How to Create Multiple Sessions
I Have a Base64 Encoded Png, How to Write the Image to a File in PHP
Adding Multiple Items to Woocommerce Cart at Once
PHP Rename Array Keys in Multidimensional Array
Codeigniter - Correct Way to Link to Another Page in a View
PHP Curl Get Request and Request's Body