Could JSON.parse have a glitch in Firefox?

While using Firefox 23.0.1, I encountered the following code snippet:

var foo = '{  "success": false,  "errtype": "barf",  "message": "my message\n"}';

var what = JSON.parse(foo);
console.log(what);

Upon running this code in the Firebug Javascript window or checking the console log on my website, I received a SyntaxError: JSON.parse: bad control character in string literal.

Despite appearing to be valid JSON according to http://www.json.org/ and passing validation at http://jsonlint.com/, the issue persisted.

Removing the "\n" from the end of "my message" resolved the problem in Firefox, although I have not tested it on other browsers yet.

Could this potentially indicate a bug within Firefox?

Answer №1

To properly handle newlines in JSON, you will need to escape the backslash in the newline with another backslash

var foo = '{  "success": false,  "errtype": "barf",  "message": "my message\\n"}';

For more information on handling newlines in JSON, check out How do I handle newlines in JSON?

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

Is it possible to use reactjs and react-router to showcase either a component or {this.props.children}?

Here's the scene: I have multiple components where certain words can be clicked to link to a reference page/component. If the highlighted words are not clicked, the component is displayed as is (and there are many of them with this feature and a menu ...

Customized webpage content using AJAX for interactive map selections

I have integrated JQVMaps into a WordPress website to display a dynamic world map. The goal is to update the content of the page based on the region that the user clicks. Below is a snippet of the code I have implemented as a proof of concept: <div ...

Troubleshooting handlebars path problem in Express.JS

https://i.sstatic.net/qEB42.jpg I have my node's logic stored in the "app" folder, where the views folder resides with templates for handlebars (express-handlebars). Inside the "config" folder, there's an express.js file where I require the exp ...

"Learn the process of tallying strings within a td table with the help of

I am looking to count the number of occurrences of a string in the column labeled status. However, I am uncertain about how to implement this logic using JavaScript. Click here for an image example ...

The light rotation issue in ThreeJS

No matter if I import the scene or create every mesh and light through code, the issue remains the same. My scene consists of a plane, a cube, and a spot light. The spot light is rotated 45 degrees on the y-axis. In the first example, it is positioned at ...

Is there a way to update the href attribute within the script section in vue.js?

I need to dynamically set the href attribute of a link based on data retrieved from a database and rendered in the methods section. <template v-if="header.value == 'ApplicationName'"> <a id="url" href="#" target="_blan ...

The req.body variable is not defined in this context, which means only the default MongoDB id is

I've been attempting to utilize a post request for inserting data into MongoDB using Mongoose, but I keep encountering an issue where req.body is undefined. The document gets inserted into the database, but only the default Object ID is included. Thi ...

Executing RegisterStartupScript repeatedly in C#

Greetings to all, Here is a snippet from my code: start code protected void Button1_Click(object sender, EventArgs e) { ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "temp", "<script>loadAdditionalInfoDialog(info1)< ...

Looking for a way to deactivate the loader after the submission is complete

Seeking to implement a loader on my asp.net webforms app. Here's my javascript loader: function loading() { $("#loading").show(); } function loaded() { $("#loading").hide(); } html loader ...

In JavaScript countdown timer, incorporate a leading zero to the minutes and seconds

I'm struggling to incorporate leading zeros into the minutes and seconds of this javascript countdown timer. I can't seem to make the leadingzero function work properly. The additional leadingzero function that I added doesn't appear to be f ...

What is the best way to navigate through a series of images using left and right arrows in a React

I need help implementing a feature where I can slide horizontally through a collection grid of 10 movie items per row, similar to Netflix grids. Is there an easy way to achieve this with arrows on the right and left ends? <div className="collection-p ...

Having trouble fetching results from AJAX objects using vanilla JavaScript?

I am currently working on developing my own AJAX prototype without relying on jQuery or any other libraries. However, I am facing an issue where I cannot retrieve any results and the breakpoint set on a specific line is not triggering: The problem seems t ...

The style of react-router links remains unvisited

Currently working on a small website using React Router, I've encountered an issue where only the homepage link receives the visited styles. The other links do not display the visited style, even after being visited. Check out the code here Here is ...

Customizing the header of a Django JsonResponse

Is there a way to update the Date header in the response using the following code snippet? > from django.http import JsonResponse If so, how can I achieve this? ...

Can nested JSON objects be created in Haskell using Text.JSON?

I have a data structure that is an object at the top level, primarily composed of strings as values and one nested object. It should appear similar to this: { "name" : "expand", "type" : "2", "code" : "...", "options" : { "atb" : { " ...

Develop a seamless user experience by creating dynamic forms using Jquery, PHP, and

I have been attempting to create a simple contact form using jQuery, AJAX, and PHP without refreshing the page. Everything seems to be working smoothly except for event.preventDefault(); Here are my files: Contact_engine.php <?php $name = $_POST ...

I attempted to log the elements of my submit button form, but unfortunately nothing is appearing in the browser console

I am attempting to capture the data from my submit button form, but for some reason, nothing is showing up in the console of my browser. $("#signupform").submit(function(event){ // Stopped default PHP processing event.preve ...

Adding properties to Vue.js component in the script section without affecting rendering

How can I import component A with props and pass it to another component B for rendering? import MyComponent from '/MyComponent.vue' computed: { compnt: function () { var comp = MyComponent // How can I set props to MyC ...

What is the best way to send a custom property through Vue router?

I'm currently working with a route instance: const router = new Router({ routes: [ { path: '/', name: 'Home', component: MainContainer, redirect: '/news/list', children: [ { ...

How come vhtml isn't displaying nested << correctly?

I am currently working on an app that utilizes dynamic binding, where <> is a way to fetch data from the API dynamically. However, I am facing an issue when trying to render it in vhtml as the parent element is not displaying at all. My goal is to ha ...