Unable to locate the Unexpected token while parsing a JSON payload

I need to show a JSON received from the server on a web page, but the JSON is in string format and seems to have been escaped during creation. Upon checking the details in the console, it throws an error:

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

Even after using various JSON validators, no invalid characters are found in the JSON.

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>

Answer №1

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

  var messages=jQuery.parseJSON(msg.replace(/\n/g,"\\n"));

  console.log(messages);
$("#message-details").html(JSON.stringify(messages));
}

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

Answer №2

Remember to use double slashes when escaping a slash

\\n

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issue with triggering blur event in Internet Explorer while using Angular 2+

The issue discussed in the Blur not working - Angular 2 thread is relevant here. I have a custom select shared component and I am attempting to implement a blur event to close it when the component loses focus. // HTML <div (blur)="closeDropDown()" t ...

Retrieving information from Freebase using PHP and JSON queries

I am in need of extracting information regarding the Production Company and Distribution Company for a specific movie by utilizing Freebase. However, I have never worked with JSON before and am unsure how to integrate the query into my PHP file. The tutor ...

Attempting to locate the following div element using a particular class name, however, I consistently retrieve the final one

I have a magician and I'm attempting to design a progress bar. The current step that the user is on should be purple and should advance as the user moves through the form. However, when trying to proceed to the next step, the progress bar incorrectly ...

What is the best way to bring a nested object to the top level without deleting the original top level

Imagine having the following dataset: data = [{ "_id" : "2fApaxgiPx38kpDLA", "profile" : { "name" : "Karina 1", "avatar" : "avatar1.jpg", "bio" ...

Guidelines for choosing a dropdown menu option using jQuery in a WordPress photo gallery

First off, take a look at the screenshot to understand what I'm trying to accomplish: Within WordPress, after clicking on "New Post" and then on the "Add Media" button, a pop-up image gallery appears with an image filtering drop-down menu. My goal is ...

Angular Search Panel

I'm having trouble with the Angular Search box not working when I try to run it locally on my computer. The same code works perfectly fine on platforms like codepen, plunker, and jsfiddle. Can someone help me figure out what's going wrong? .ex ...

Issue with encoding long strings in json_encode

Could my JSON encoding script be encoding strings instead of causing another issue? First, let me display all the configurations and codes I have: Table Structure: CREATE TABLE `dc_songs` ( `songs_id` int(11) NOT NULL AUTO_INCREMENT, `username` varcha ...

npm not only loads the packages specified in my package.json file

Currently, I am working on a small project utilizing npm, bower, and grunt. Upon executing "npm install" on my personal computer, it seems to be loading an excessive amount of peculiar items (refer to attached screenshot). However, when performing the same ...

Having issues with getting Bootstrap to function properly in my create-react-app

I've been attempting to use traditional Bootstrap instead of react-bootstrap to render my React component, but I can't seem to get it right. I've installed Bootstrap via npm and included the CDN links and scripts as well. Despite trying vari ...

Refresh meta tags in a Next.js/React app

In the process of optimizing a website for SEO, I am facing the challenge of updating the meta description's content dynamically without using any third-party libraries. While I have successfully implemented the functionality, it currently requires a ...

Unable to retrieve data from Express API using React frontend

After three weeks of struggling to fetch a JSON response from an Express API into a React App, I am reaching out for help. Despite spending over 40 hours on tutorials, I still haven't been able to make it work. While I know this might be redundant, I ...

The art of bringing a pseudo class to life through animation

Seeking assistance with achieving a unique effect on click event. I have set up a slanted or razor-blade style div using a parent div element and a pseudo-element :after (as shown below). My goal is to change the skew angle when the user clicks on the pare ...

jQuery color picker plug-in for selecting and displaying a range of colors

I am currently developing a program that utilizes HTML, CSS, JavaScript, and JQuery for its user interface. One essential feature of this UI is the ability for users to choose one option from a set list of colors. It is crucial that each element in this co ...

The dynamic loading of select tag options in Vue.js is not rendering properly

I'm having trouble displaying a select tag with options loaded from a Vue object. However, when I try to render it, something seems off: https://i.sstatic.net/odbC6.png Here is the HTML markup for the select tag: <div class="form-group ui-model" ...

Creating independent Javascript applications and HTML5 canvas games, optimized for V8 or tailored to the specific browser for optimal performance

Instead of solely targeting Chrome users due to browser performance issues, I am exploring alternative options to create an executable version of a canvas game. Is there a way to integrate v8 for non-Chrome users? Perhaps a web "wrapper" similar to Flash&a ...

Creating a RequestBody from a JSONObject: A Step-by-Step Guide

When using okhttp3.RequestBody to send a request to the server and having a JSONObject with data that needs to be sent, you can write code similar to the following: RequestBody requestBody = new MultipartBody.Builder() .set ...

Trouble with database updates in Mongodb using Node.js: issues with ".findOneAndUpdate()" operation

When attempting to update my database using .findOneAndUpdate(), I encountered an issue where the embedded document competitorAnalysisTextData remained empty despite no error messages. // on routes that end in /users/competitorAnalysisTextData // -------- ...

displaying a confirmation alert upon unchecking a checkbox using javascript

I am trying to implement a feature where a message saying "are you sure?" pops up when the user tries to uncheck a checkbox. If they choose "yes", then the checkbox should be unchecked, and if they choose "no" it should remain checked. I'm new to Java ...

Obtaining entry to an ArrayList instance

Currently, I'm working on an application that processes JSON data to generate statistics. However, I've encountered a roadblock in my progress. The JSON data structure I'm dealing with looks like this: { "position":[ { "someKey1":"s ...

What is the best way to automatically focus on the initial input field in a form after clicking the submit button?

Check out this HTML form: <form> <input type="text" id="input1"> <input type="text"> <button>Submit</button> </form> When clicked, I want the focus to move to #input1 from the S ...