JSON Parse error: Unterminated string
Asked Answered
N

5

13

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.

Noreennorene answered 26/12, 2014 at 11:17 Comment(1)
this one is valid. try here jsonlint.comUnspoiled
H
13

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.

Hirsute answered 26/12, 2014 at 11:23 Comment(1)
@Naveen - The thing is, the string is first parsed in javascript as a string literal, where one of the escapes is removed, and after that it's valid JSON, with one escape remaining.Hirsute
P
0
    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..

Phlebotomize answered 26/12, 2014 at 11:26 Comment(0)
R
0

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;
Rawden answered 17/12, 2018 at 22:29 Comment(0)
K
0

okay finally i fixed it so the problem with mine was the the size of the file i was trying to parse with json.parse() the maximum amount that json.parse() can parse is 5368709121 bytes so if the problem with yours is this check with different assets.

Krilov answered 11/4 at 10:0 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewMokas
A
-1

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

\"
\'
Acarid answered 26/12, 2014 at 11:33 Comment(2)
The un-encoded object contains test" on index 14, not test'.Sprinkling
you can use \\ to escape "Acarid

© 2022 - 2024 — McMap. All rights reserved.