JSON Parse error: Unterminated string - javascript

I've met an usual problem when escaping a quote within the JSON parse function. If the escaped quote is present, in this case 'test"', it causes the following error 'SyntaxError: JSON Parse error: Unterminated string'.
var information = JSON.parse('[{"-1":"24","0":"","1":"","2":"","3":"0.0000","4":"","5":"0.00","6":"0.00","7":"1.00","8":"0","9":"false","10":"false","11":[""],"12":"","13":"","14":"test\""}]');
JSON Lint validates the JSON as valid.

You'll have to double escape it, as in "test\\""
var information = JSON.parse('[{"-1":"24","0":"","1":"","2":"","3":"0.0000","4":"","5":"0.00","6":"0.00","7":"1.00","8":"0","9":"false","10":"false","11":[""],"12":"","13":"","14":"test\\""}]');
document.body.innerHTML = '<pre>' + JSON.stringify(information, null, 4) + '</pre>';
The first backslash escapes the second backslash in the javascript string literal.
The second backslash escapes the quote in the JSON string literal.
So it's parsed twice, and needs escaping twice.
So even if it's valid JSON, you'll need one escape for javascript string literals that escapes the escape used in JSON.

var information = JSON.parse('[{"-1":"24","0":"","1":"","2":"","3":"0.0000","4":"","5":"0.00","6":"0.00","7":"1.00","8":"0","9":"false","10":"false","11":[""],"12":"","13":"","14":"test\\""}]');
Putting doubleslash worked for me...at least in console..give it a try..

I fixed this problem in my spring boot app with #RestController by Using #IgnoreJson
--------- class book ---------------
#OneToMany(mappedBy = "book")
private Set<Chapitre> chapitres = new TreeSet<>();
--------- class chapitre -----------
#ManyToOne(cascade = CascadeType.MERGE)
#JsonIgnore
private Book book;

Here is the issue : use \' instead of \" at last array of object
\"
\'

Related

JSON.parse(), why this escape character returns an error?

I am trying to learn more about the core functionality of the JSON.parse() method, but I cannot understand why this string returns an error:
JSON.parse('["foo\\"]'); // > "Unexpected end of input"
Why does this not return ["foo\"]?
Because the string isn't terminated, because the second " is escaped by \, because the data you're parsing is:
["foo\"]
You can confirm this yourself with
var s = '["foo\\"]';
alert(s); // or
console.log(s);
This is because of the cumulative escape action of \ in JS and JSON level in your expression.
Your string ["foo\\"]
Escaped by JS to be ["foo\"] first because its double \\
Now it is sent to JSON.parse like ["foo\"] which escapes the "
So, it is an un-terminated string for JSON parser.
If you need to parse ["foo\\"] in json level, assemble the js string to be escaped to this. Which is ["foo\\\\\"].
TEST.
s = '["foo\\\\\"]';
console.log(s); //'["foo\\"]'
console.log(JSON.parse(s);) // ["foo\"]

How to correctly handle an Unterminated string exception?

I'm using the JSON.org java library to tinker with.
At the moment I'm encountering an error because of \n characters:
try{
String s = "{\"property:\":\"line1\n,line2\"}";
JSONObject o = new JSONObject(s);
}catch(Exception e){
e.printStackTrace(System.err);
}
results in:
org.json.JSONException: Unterminated string at 20 [character 0 line 2]
at org.json.JSONTokener.syntaxError(JSONTokener.java:433)
at org.json.JSONTokener.nextString(JSONTokener.java:261)
at org.json.JSONTokener.nextValue(JSONTokener.java:361)
at org.json.JSONObject.<init>(JSONObject.java:218)
at org.json.JSONObject.<init>(JSONObject.java:325)
After doing a quick search I found this answer
which points to my string having to be escaped like so:
String s = "{\"property:\":\"line1\\n,line2\"}";
I would no longer see the exception and could do a string replace for "\n" with '\n', but I'm just wondering:
is the recommended way of dealing with new lines in a JSON string in java ?
As you can see in the JSON specification, JSON does not allow real line breaks in strings, so you always need to escape newline characters with \n. This is not a special Java thing. See also this question.

Json parse() does not escape \" in javascript

I have java object which is converted as JSON string using
String paramMap = new ObjectMapper().writeValueAsString(custPolicy.getParamMap());
model.addAttribute("testTypeMap", paramMap );
In the .jsp page, on load I'm trying to parse the testTypeMap and get object back;
var paramMap = JSON.parse('${testTypeMap}');
showTestType('File content', 'LINUX', paramMap);
The object has double quotes (") in one of the fields, and it is escaped with backslash () when it is converted as JSON sting in java, that is why we see "\"" (from view source)
var paramMap = JSON.parse('{"Filepath":"/home/status.txt","Search expression":"\""}');
But the above line says, "Uncaught SyntaxError: Unexpected string".
I have seen few posts and they say it need two parses, one for javascript and one for JSON. I tried to replace \" with \\" ; but in javascript \" is always ", so I could not replace it;
Any pointer for what I miss here?
The problem is that you're not encoding the string in ${testTypeMap} as a JavaScript literal. I'm unsure how to do it specifically in your framework, but it's akin to HTML encoding a string, but for JavaScript instead.
However!
In your specific example you can avoid using JSON.parse because JSON is already in a format consumable by JavaScript.
var paramMap = ${testTypeMap};
showTestType('File content', 'LINUX', paramMap);
With the resulting source sent to browser looking like:
var paramMap = {"Filepath":"/home/status.txt","Search expression":"\""};
Actually I was not knowing how to replace \" with \\" in javascript, as \" is always represented as " (just one quote without backslash).
So I did this replace in server side after converting to a JSON string using Jackson's ObjectMapper as below:
String paramMap = new ObjectMapper().writeValueAsString(custPolicy.getParamMap());
// need to replace any \" with \\" in javascript side
paramMap = paramMap.replace("\\\"", "\\\\\"");
model.addAttribute("testTypeMap", paramMap );
Now in client-side it shows as below:
var paramMap = JSON.parse('{"Filepath":"/home/cavirin/status.txt","Search expression":"\\""}');
and this works fine as javascript parse is already taken care in server side.

JSON.parse error is giving error with double quotes string

Why is it that
//Code
JSON.parse("{'name':'Khushal Khan'}");
results in this error
//Resposnse
SyntaxError: Unexpected token '
while this works perfectly
//Code
JSON.parse('{"name":"Khushal Khan"}');
Output:
//Response
Object {name: "Khushal Khan"}
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:
The problem is not that your JavaScript string uses " characters but that your JSON strings do not.
JSON is not JavaScript. JSON strings must be delimited by " characters.
From the specification:
string = quotation-mark *char quotation-mark
and
quotation-mark = %x22 ; "
Problem is not with double quoted string, but the json should have no single quotes as delimiters.

jQuery.parseJSON throws “Invalid JSON” error due to escaped single quote in JSON

I’m making requests to my server using jQuery.post() and my server is returning JSON objects (like { "var": "value", ... }). However, if any of the values contains a single quote (properly escaped like \'), jQuery fails to parse an otherwise valid JSON string. Here’s an example of what I mean (done in Chrome’s console):
data = "{ \"status\": \"success\", \"newHtml\": \"Hello \\\'x\" }";
eval("x = " + data); // { newHtml: "Hello 'x", status: "success" }
$.parseJSON(data); // Invalid JSON: { "status": "success", "newHtml": "Hello \'x" }
Is this normal? Is there no way to properly pass a single quote via JSON?
According to the state machine diagram on the JSON website, only escaped double-quote characters are allowed, not single-quotes. Single quote characters do not need to be escaped:
Update - More information for those that are interested:
Douglas Crockford does not specifically say why the JSON specification does not allow escaped single quotes within strings. However, during his discussion of JSON in Appendix E of JavaScript: The Good Parts, he writes:
JSON's design goals were to be minimal, portable, textual, and a subset of JavaScript. The less we need to agree on in order to interoperate, the more easily we can interoperate.
So perhaps he decided to only allow strings to be defined using double-quotes since this is one less rule that all JSON implementations must agree on. As a result, it is impossible for a single quote character within a string to accidentally terminate the string, because by definition a string can only be terminated by a double-quote character. Hence there is no need to allow escaping of a single quote character in the formal specification.
Digging a little bit deeper, Crockford's org.json implementation of JSON for Java is more permissible and does allow single quote characters:
The texts produced by the toString methods strictly conform to the JSON syntax rules. The constructors are more forgiving in the texts they will accept:
...
Strings may be quoted with ' (single quote).
This is confirmed by the JSONTokener source code. The nextString method accepts escaped single quote characters and treats them just like double-quote characters:
public String nextString(char quote) throws JSONException {
char c;
StringBuffer sb = new StringBuffer();
for (;;) {
c = next();
switch (c) {
...
case '\\':
c = this.next();
switch (c) {
...
case '"':
case '\'':
case '\\':
case '/':
sb.append(c);
break;
...
At the top of the method is an informative comment:
The formal JSON format does not allow strings in single quotes, but an implementation is allowed to accept them.
So some implementations will accept single quotes - but you should not rely on this. Many popular implementations are quite restrictive in this regard and will reject JSON that contains single quoted strings and/or escaped single quotes.
Finally to tie this back to the original question, jQuery.parseJSON first attempts to use the browser's native JSON parser or a loaded library such as json2.js where applicable (which on a side note is the library the jQuery logic is based on if JSON is not defined). Thus jQuery can only be as permissive as that underlying implementation:
parseJSON: function( data ) {
...
// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
...
jQuery.error( "Invalid JSON: " + data );
},
As far as I know these implementations only adhere to the official JSON specification and do not accept single quotes, hence neither does jQuery.
If you need a single quote inside of a string, since \' is undefined by the spec, use \u0027 see http://www.utf8-chartable.de/ for all of them
edit: please excuse my misuse of the word backticks in the comments. I meant backslash. My point here is that in the event you have nested strings inside other strings, I think it can be more useful and readable to use unicode instead of lots of backslashes to escape a single quote. If you are not nested however it truly is easier to just put a plain old quote in there.
I understand where the problem lies and when I look at the specs its clear that unescaped single quotes should be parsed correctly.
I am using jquery`s jQuery.parseJSON function to parse the JSON string but still getting the parse error when there is a single quote in the data that is prepared with json_encode.
Could it be a mistake in my implementation that looks like this (PHP - server side):
$data = array();
$elem = array();
$elem['name'] = 'Erik';
$elem['position'] = 'PHP Programmer';
$data[] = json_encode($elem);
$elem = array();
$elem['name'] = 'Carl';
$elem['position'] = 'C Programmer';
$data[] = json_encode($elem);
$jsonString = "[" . implode(", ", $data) . "]";
The final step is that I store the JSON encoded string into an JS variable:
<script type="text/javascript">
employees = jQuery.parseJSON('<?=$marker; ?>');
</script>
If I use "" instead of '' it still throws an error.
SOLUTION:
The only thing that worked for me was to use bitmask JSON_HEX_APOS to convert the single quotes like this:
json_encode($tmp, JSON_HEX_APOS);
Is there another way of tackle this issue? Is my code wrong or poorly written?
Thanks
When You are sending a single quote in a query
empid = " T'via"
empid =escape(empid)
When You get the value including a single quote
var xxx = request.QueryString("empid")
xxx= unscape(xxx)
If you want to search/ insert the value which includes a single quote in a query
xxx=Replace(empid,"'","''")
Striking a similar issue using CakePHP to output a JavaScript script-block using PHP's native json_encode. $contractorCompanies contains values that have single quotation marks and as explained above and expected json_encode($contractorCompanies) doesn't escape them because its valid JSON.
<?php $this->Html->scriptBlock("var contractorCompanies = jQuery.parseJSON( '".(json_encode($contractorCompanies)."' );"); ?>
By adding addslashes() around the JSON encoded string you then escape the quotation marks allowing Cake / PHP to echo the correct javascript to the browser. JS errors disappear.
<?php $this->Html->scriptBlock("var contractorCompanies = jQuery.parseJSON( '".addslashes(json_encode($contractorCompanies))."' );"); ?>
I was trying to save a JSON object from a XHR request into a HTML5 data-* attribute. I tried many of above solutions with no success.
What I finally end up doing was replacing the single quote ' with it code ' using a regex after the stringify() method call the following way:
var productToString = JSON.stringify(productObject);
var quoteReplaced = productToString.replace(/'/g, "'");
var anchor = '<a data-product=\'' + quoteReplaced + '\' href=\'#\'>' + productObject.name + '</a>';
// Here you can use the "anchor" variable to update your DOM element.
Interesting. How are you generating your JSON on the server end? Are you using a library function (such as json_encode in PHP), or are you building the JSON string by hand?
The only thing that grabs my attention is the escape apostrophe (\'). Seeing as you're using double quotes, as you indeed should, there is no need to escape single quotes. I can't check if that is indeed the cause for your jQuery error, as I haven't updated to version 1.4.1 myself yet.

Categories

Resources