Utilizing the JSON.parse method in JavaScript in conjunction with an Ajax request while incorporating the escape character "\\n" for new line functionality

https://jsbin.com/zuhatujoqo/1/edit?js,console

Edit: json file has this line:

{"pd":"ciao \\n ste"}

I need to retrieve a valid JSON file through an ajax call.
Then parse the result using JSON.parse.

I'm confused about the behavior of the "\\n" escape character found in my JSON file.

When I use JSON.parse with the same values, it gives me different results.

var result = JSON.parse(data);
console.log(result);
var result2 = JSON.parse('{"pd":"ciao \\n ste"}');
console.log(result2);

My understanding is that JavaScript might be escaping (or unescaping?) the string before parsing it.

The issue arises when I try to do this:

result = result.replace(/\\n/g, "<br />");

Do I also need to escape the regex itself?

In summary: I want to load the .json file via Ajax. I expect JSON.parse to properly handle the "\\n" by converting it into a NewLine character.

Answer №1

Special thanks to Bergi for helping me realize that the issue was not with JSON.parse, but rather with the format of my json file. I mistakenly had "\\n" instead of "\n".

Check out this updated jsbin to see how JSON.parse now functions consistently across different scenarios: https://jsbin.com/xezipucili/1/edit?js,console

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

Breaking down JSON data frames into various columns in R

I have a JSON file that I need to process data = readLines("D:/aaa/bbb/ccc/ddd.json") data = lapply(data, fromJSON) data = lapply(data, unlist) result = bind_rows(data) Upon processing the JSON file with readLines method, I obtained this result: ...

How can you use Vue.js @mouseover to target a specific <path> element within an <svg>?

Check out this Codepen example. I am working with an SVG map that contains various paths holding data. My goal is to retrieve the state name when hovering over a specific path. Currently, I have added an event listener to the svg element and am trying to ...

Recording Audio Using ReactJS

I am exploring ways to record audio using ReactJS and save it on my Node server. Initially, I attempted to utilize the "react-audio-recorder" module but encountered issues when attempting to record multiple audios consecutively. Additionally, I experiment ...

How can I use jQuery to access the parent node in an XML document?

I have been trying to extract the top-level 'label' attribute from the XML code below using jQuery, but I haven't had any luck so far. I have already converted it into a DOM object, but the results are not what I expected. Does anyone have a ...

The AJAX Contact Form seems to be malfunctioning

I've encountered a persistent issue with my AJAX Contact Form, and all attempts to resolve it have been unsuccessful. The error message from the jQuery section if(html==0) keeps appearing. If anyone can offer assistance in identifying and fixing this ...

Exploring the integration of React Context API within a Next.js application to streamline the authentication process

I am looking to build a react app using Next.js. However, I am currently stuck and need assistance in figuring out how to proceed. I have implemented user authentication on the backend with node.js, passport.js, passport-local-mongoose, and express.sessi ...

Use PipeTransform to apply multiple filters simultaneously

Is it possible to apply multiple filters with PipeTransform? I attempted the following: posts; postss; transform(items: any[]): any[] { if (items && items.length) this.posts = items.filter(it => it.library = it.library ...

What could be causing my SVG bezier curves to malfunction in Firefox?

Encountered an issue today where diagrams I have generated are not functioning properly in Firefox when created using the getPointAtLength method. Here is a demonstration of the problem: http://jsfiddle.net/xfpDA/9/ Please take note of the comments at th ...

Exploring JSON Data with the Wikipedia API

My current project involves extracting text from Wikipedia articles using their API, which is not the most user-friendly tool. I am facing challenges with parsing the JSON object that I receive in return. The key containing the desired text is labeled &apo ...

Using Ruby instead of Javascript in lessc

My CSS is compiled in a node application using lessc. LESS was installed via NPM. When I check the current version of lessc --version it shows lessc 1.3.3 (LESS Compiler) [Ruby] 2.3.2 How can I change it to display lessc 1.3.0 (LESS Compiler) ...

Creating a search bar with React-Redux: A step-by-step guide

Hey everyone, I've got this component here: const Iphone = ({phones,searchQuery}) => { const filterIphone = phones.map((p, index) => (<div className="model" key={index}> <NavLink to={'/p/' + p.id}>{p.body.mod ...

The second argument in the "JSON_VALUE or JSON_QUERY" function must be a string literal, according to their requirements

I have a JSON object that I need to pass to a stored procedure in SQL Server. { "individual": [ { "app_id": 1057029, "size": 2 }, { "app_id": 1057053, ...

What steps can I take to avoid page displacement when implementing jQuery ajax tabs?

There are numerous inquiries regarding this issue, and it appears that some are very similar to what I am looking for, such as: JQuery UI Tabs Causing Screen to "Jump" Stop jquery TABS from jumping / scrolling up when clicked on? Jquery tabs: How do I ...

Validate that a string is a correct file name and path in Angular without using regular expressions

Currently, I am using regex to determine if a string is a valid file name and path. However, when the string length becomes longer, it ends up consuming a significant amount of CPU, resulting in browser performance issues. public static readonly INVALID_F ...

Using Mysqli prepared statements in conjunction with AJAX POST to a PHP script

My query is regarding the efficiency of PHP Mysqli prepared statements. Based on my understanding from basic research, prepared statements offer enhanced security by using bound inputs and optimize the process by 'pre-packaging' SQL queries to so ...

send parameter when clicking a link rather than using a hashtag

One interesting feature on my website is a menu link with the attribute title=.roc. When this link is clicked, I want to extract this attribute and click on an element with the same attribute on the destination page. Let's consider a scenario where I ...

Creating a Dynamic Dropdown Menu in Rails 4

I am attempting to create a dynamic selection menu following this tutorial; however, I am encountering issues as the select statement does not seem to be updating. Below is the code snippet I currently have: #characters_controller.rb def new ...

Customize Tab indicator styling in Material-UI

Currently, I am attempting to customize the MUI tab by changing the indicator from a line to a background color. Through my research, I discovered that using TabIndicatorProps and setting a style of display:none eliminates the indicator completely. However ...

Transmit all Form Data via WordPress ajax

<?php add_action( 'admin_footer', 'my_action_javascript' ); function my_action_javascript() { ?> <script type="text/javascript" > jQuery(document).ready(function($) { var data = { action: 'my_action', ...

The retrieved JSON from the API endpoint is not consistent

My test class contains 4-5 tests, one of which fetches JSON from a specified URL. When I run that particular test alone, the output is as follows: {"result":[{"result":[...]}]} However, when I run all the tests in the class, the same test produces the fo ...