1

I have a JSON file and i am getting the json content which may contain new lines in it. Now i am facing a problem. I am getting the below error :

Unexpected token ↵ in JSON at position 9 

Note: the error message says specificaly this symbol: '↵'. What does this mean?

My JSON:

{"T":". ^00:00:43^2008-09-11 12:00:00.0"} 

But if i remove the new line (not sure if its a new line or a tab space no matter what i am facing this issue even if its a tab space or new line. It works fine with normal space.). Below is the modified JSON and it looks good.

Modified JSON:

 {"T":".^00:00:43^2008-09-11 12:00:00.0"} 

I have tried to follow similar threads like here and here and some similar threads. but nothing works even after applying those. Any help is appreciated here please.

This is one the solution i have tried:

var jsonString = JSON.stringify(data).replace(/(\r\n|\n|\r)/gm,""); 

Is there anyway to detect these linebreaks and tab spaces and remove them?

10
  • About the , it's a symbol used to visualize a newline. Commented Jan 4, 2018 at 2:37
  • @PredatorIWD so in this case its teh new line which is causing the problem in the jsonstring? Commented Jan 4, 2018 at 2:38
  • What does yourString.charCodeAt(9) show? Commented Jan 4, 2018 at 2:38
  • I believe in the above JSon 8th char is space and 9th is a new line here..I tried to replace new lines with normal space but thats not working either. @Barmar. So just trying to take suggestions here Commented Jan 4, 2018 at 2:39
  • 1
    Post your code, otherwise we're just guessing at what you might be doing wrong. Commented Jan 4, 2018 at 2:42

3 Answers 3

4

You shouldn't be calling JSON.stringify(). That will convert the newlines in the string to literal \n, which won't be matched by the escape sequences in the regexp. Just do the replacement directly on the bad data.

var data = `{"T":". ^00:00:43^2008-09-11 12:00:00.0"}`; var jsonString = data.replace(/(\r\n|\n|\r)/g,""); var object = JSON.parse(jsonString); console.log(object);

Sign up to request clarification or add additional context in comments.

6 Comments

i have the above suggestion. But still it says the same message. Not sure why is it showing even if we have removed new line char's :(
I am getting the result as a AJAX call. Do you think that is the reason its not working in that case? @Barmar
I don't think that should make a difference. A newline is a newline.
zach mentioned this in his comment : "Not on browser Javascript side. Formatting has to be done before it reaches browser Javascript." for my question : is there a way to detect new line chars and replace them in ajax call.
That's not true. It only has to be fixed before calling JSON.parse(). There's nothing wrong with returning newlines in the response text.
|
1

This worked for me:

var jsonString = data.replace(/[\n\r\t]/g,""); 

Comments

0

That is not a valid JSON value. If you refer to https://www.json.org/, newline, carriage returns and etc will need to be included in the value itself.

There is no way to modify because the parser already throws the error.

The only solution I can think of is for the author of the JSON file to amend their value like your Modified JSON or if they want to preserve that newline,

{"T":".\n^00:00:43^2008-09-11 12:00:00.0"} 

5 Comments

Thanks. is there a way to detect new line chars and replace them with \n? @zack
Not on browser Javascript side. Formatting has to be done before it reaches browser Javascript.
so there is no way to check for new line chars from the ajax call response? :O not sure what should be the solutions here :(
Or you can read the file with Ajax and treat the response data as a regular text/plain mime type first. From there, you can do those parsing and cleaning as mentioned by other answers. Then do JSON.parse().
Hi Barmar, sorry I dont have reputation to comment in your answer. In your code snippet, you added the ` quote which OP's value doesn't contain that back quote. I assume he fetch the JSON file as it is. My earlier comment is in agreement with your explanation; Treat as regular text/plain first and do the parsing on the string, then parse to JS object.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.