Can not find Unexpected token when parsing a JSON string

Viewed 117

I am going to display a received JSON (but received as string) from server in a webpage. The json seems escaped when it was being created. When I check details in console it says

    Uncaught SyntaxError: Unexpected token   in JSON at position 67
    at JSON.parse (<anonymous>)
    at displaySmsDetails (sms.html:108)
    at <anonymous>:1:1

I have checked the json in json validators but they find no invalid character.

function displaySmsDetails(){
 var temp='[{"date":"1589952101314","number":"989999920000","body":"بانك سامان\nبرداشت مبلغ 100,000 خريدکالا\nاز ‪873-1‬\nمانده 1,676\n1399/2/31\n09:51:35","type":"inbox"}]';
 var list=JSON.parse(temp);
 $("#details").html(JSON.stringify(list));
}

displaySmsDetails()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="details"></div>

2 Answers

function displaySmsDetails(){
 var temp='[{"date":"1589952101314","number":"989999920000","body":"بانك سامان\nبرداشت مبلغ 100,000 خريدکالا\nاز ‪873-1‬\nمانده 1,676\n1399/2/31\n09:51:35","type":"inbox"}]';

  var list=jQuery.parseJSON(temp.replace(/\n/g,"\\n"));
 
  console.log(list);
 $("#details").html(JSON.stringify(list));
}

displaySmsDetails()
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<div id="details"></div>

You should escape the slash with another slash

\\n
Related