"Converting a string to a JSON object results in an error: Unexpected

Trying to convert a string containing escape characters into JSON is resulting in an error being thrown.

  let str = '[{"name":"content","readonly":false,"value":"<div class=\"blueheading\"><h2>Free Gifts for Him</h2><h3><a href=\"http://abcd.com/free-gifts-with-purchase-for-him.aspx\">View all for Him</a></h3></div>"},{"name":"cached","readonly":true,"value":true}]';
            let myObj = JSON.parse(str);

Answer №1

If you need to include a double quotation mark (`"`) in a string, you must escape it with a backslash (`\`) like this: '\\"'.

Here is an example:

var str = '[{"name":"content","readonly":false,"value":"<div class=\\"blueheading\\"><h2>Free Gifts for Him</h2><h3><a href=\\"http://abcd.com/free-gifts-with-purchase-for-him.aspx\\">View all for Him</a></h3></div>"},{"name":"cached","readonly":true,"value":true}]';
var myObj = JSON.parse(str);

Learn more about special characters in string literals here

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

What is the best way to dynamically change the color of my component depending on the prop passed to it?

I am facing an issue with the color of my component changing based on the value of the prop 'level'. Despite using states to set the backgroundColor, all components end up having the same color due to the state being altered for every comment. I ...

Having trouble deactivating date selections in Vue.js Material Datepicker

Can someone assist with the following HTML code: <datepicker :date="child" :option="option" v-model="child.dob" @change="updatechildDOB(child)" :disabled="disabled"></datepicker> Here are the available options: option: { type: 'd ...

Executing a SQL query using a JavaScript variable as a parameter

I am currently working on a website form that includes a select menu populated with data from an SQL table using a loop. The form is being displayed using JavaScript scripts, which are functioning perfectly. However, I am facing an issue in the final step ...

How can you efficiently send JSON data from Ajax to a Servlet in a Java environment?

I am encountering a perplexing issue with sending data from my jQuery script to a servlet using AJAX. The strange part is that when I set the contentType to application/json, all values on the server side turn out to be null. However, if I remove it, the s ...

Unexpected error in log4javascript on IE8: Expected function not found

I'm attempting to redirect console calls to the log4javascript library. Essentially, any usage of console.log should trigger log.info, with log being an instance of Log4javascript. However, when log.info is invoked, I encounter a "Fonction attendue" ...

Update a specific line in a file with node.js

What is the most efficient way to replace a line in a large (2MB+) text file using node.js? Currently, I am accomplishing this by Reading the entire file into a buffer. Splitting the buffer into an array by the new line character (\n). Replacing th ...

What is the best way to transfer functions connected to an Object over to Object.prototype?

Imagine having this: var exampleObject = {age: 25, name: 'John'}; If you do this: Object.keys(exampleObject); // it will return ['age', 'name'] Now, what if you want to add this functionality to the object prototype? You c ...

Tips for utilizing ng class within a loop

Having some trouble with my template that loops through a JSON file using json server. The issue I'm facing is related to correctly applying ng class when clicking on icons. Currently, when I click on an icon, it adds a SCSS class but applies it to al ...

Defining a Global Variable using the jQuery $(this)

I have been looking for ways to simplify my coding process, and one method I've tried is assigning a global variable. var parent = $(this).parent().parent().parent(); var parentModule = $(this).parent().parent().parent().parent(); Throughout my code ...

iPhone experiencing no response to HTTPS AJAX request

$(function () { var apiEndpoint = "https://www.myaddress.com/api/"; var getVersion = $.ajax({ url: apiEndpoint + "version/", dataType: "JSON", username: "myuser", type: "GET", password: "mypass" }); ...

The evaluate function is not functioning as expected

Code: console.log(propertyName); console.log(eval(this.state.propertyName)) console.log(this.state.DriverFirstName); Output: DriverFirstName undefined fsdfds I am attempting to access a variable defined by a string value (propertyNa ...

Develop a unique method for loading AngularJS templates

When working with AngularJS, there are various ways to provide an external template, such as using a script tag or a separate HTML file on the web server. However, I am faced with a situation where I need to implement a custom logic for retrieving these ...

Using the Loop Function in Node.js with EJS Templates

Seeking help with a node.js application that utilizes bootstrap. I am trying to display the bootstrap cards in rows of 3, with each row containing data from my dataset in columns. However, my current implementation using two for loops is leading to repeate ...

Unable to execute click events on JavaScript functions after updating innerHTML using PHP and Ajax

To begin, I want to clarify that I am intentionally avoiding the use of jQuery. While it may simplify things, it goes against the purpose of my project. Please note that 'ajaxFunction' serves as a generic example for AJAX requests using GET/POST ...

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 ...

Selecting objects within a small three.js view

I am able to showcase an entire page filled with graphical elements using three.js and can even select objects by clicking on them. However, when attempting to display three.js graphics in a small viewport within an HTML page, issues arise. In order to ca ...

Issue with Chrome not triggering onMouseEnter event when an element blocking the cursor disappears in React

Important Note: This issue seems to be specific to Chrome Currently, React does not trigger the onMouseEnter event when a blocking element disappears. This behavior is different from standard JavaScript events and even delegated events. Below is a simpli ...

Tips for making an image box with a rollover effect

I need help figuring out how to create a unique user experience within a container that is 500px wide and 800px tall. The container currently has an image as a background, and I want to add a "sign up" button in the middle. When this button is clicked, I w ...

Guidelines for Jersey to decode JSON using Jackson's subtype deserialization technique

When I receive JSONs, they come in this format: @POST @Path("log") public Map<String, List<OperationResult>> log(Stats stats) { .. } Here are examples of the JSONs I receive: { "eventType": 1 "params": { "field1" : 10 } } { "e ...

Unable to maintain constant slide down feature for jQuery dropdown

I am currently working on a website for a client and I am still learning the ropes of jQuery. The website required a dropdown menu to display links to galleries in a list format instead of having them all on the navigation bar. However, I encountered an is ...