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?

Barmar
  • 741,623
  • 53
  • 500
  • 612
Stack Acc
  • 59
  • 1
  • 2
  • 9
  • About the `↵`, it's a symbol used to visualize a newline. – Luka Čelebić Jan 04 '18 at 02:37
  • @PredatorIWD so in this case its teh new line which is causing the problem in the jsonstring? – Stack Acc Jan 04 '18 at 02:38
  • What does `yourString.charCodeAt(9)` show? – Barmar Jan 04 '18 at 02: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 – Stack Acc Jan 04 '18 at 02:39
  • If it's a newline then the solutions in the other questions should work. That's why I wanted to see the code, maybe it's some other special character. – Barmar Jan 04 '18 at 02:41
  • Are you sure you're saving the result of `.replace()` back into the variable? It doesn't modify the string in place. – Barmar Jan 04 '18 at 02:41
  • 1
    Post your code, otherwise we're just guessing at what you might be doing wrong. – Barmar Jan 04 '18 at 02:42
  • Did you just replace `\n`? You may also have to replace carriage return (`\r`). – c1moore Jan 04 '18 at 02:54
  • Hi @Barmar. This is one the solution i have tried: var jsonString = JSON.stringify(data).replace(/(\r\n|\n|\r)/gm,""); and similarly other replace()'s. – Stack Acc Jan 04 '18 at 02:55
  • Never-mind that comment I deleted. I was thinking of `parse`, not `stringify`. – Useless Code Jan 04 '18 at 03:49

3 Answers3

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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 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 :( – Stack Acc Jan 04 '18 at 03:16
  • I am getting the result as a AJAX call. Do you think that is the reason its not working in that case? @Barmar – Stack Acc Jan 04 '18 at 03:35
  • I don't think that should make a difference. A newline is a newline. – Barmar Jan 04 '18 at 03:36
  • 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. – Stack Acc Jan 04 '18 at 03:38
  • 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. – Barmar Jan 04 '18 at 03:39
  • Make sure you're not using `dataType: 'json'` in your `$.ajax` call. You need to get the text so you can fix it before trying to parse. – Barmar Jan 04 '18 at 03:39
1

This worked for me:

var jsonString = data.replace(/[\n\r\t]/g,"");
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"}
zack
  • 11
  • 2
  • Thanks. is there a way to detect new line chars and replace them with \n? @zack – Stack Acc Jan 04 '18 at 02:57
  • Not on browser Javascript side. Formatting has to be done before it reaches browser Javascript. – zack Jan 04 '18 at 03:11
  • 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 :( – Stack Acc Jan 04 '18 at 03:18
  • 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(). – zack Jan 04 '18 at 03:23
  • 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. – zack Jan 04 '18 at 06:30