What is the best way to handle parsing JSON with special characters in JavaScript?

Content stored in my database:

"Recommended cutting conditions"

When using Json_encode in PHP, the result is:

{"table1":[{"Item":{"original_text":"\u63a8\u5968\u5207\u524a\u6761\u4ef6 \b"}}]};

In JavaScript :

var strData ='{"table1":[{"Item":{"original_text":"\u63a8\u5968\u5207\u524a\u6761\u4ef6 \b"}}]}';//getData();
strData=strData.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");      
var jsonData = JSON.parse(strData)

Error encountered:

Uncaught SyntaxError: Unexpected token  in JSON at position 56
    at JSON.parse (<anonymous>)

https://i.sstatic.net/xQ901.png How can I parse JSON with special characters?

Thank you all.

Answer ā„–1

One task involves copying JSON data to JavaScript.

For some reason, it may seem necessary to perform this action, but in actuality, there is a simpler way. You don't have to treat the JSON data as a string within your JavaScript code. JSON itself is a valid type of data in JavaScript. Therefore, you can directly insert your JSON data into your JavaScript code without using quotes:

var info = {"table1":[{"Item":{"original_text":"\u63a8\u5968\u5207\u524a\u6761\u4ef6 \b"}}]};

The above code block represents a valid JavaScript object that does not require any special parsing techniques.

If you specifically need a valid JavaScript string containing JSON data, you can achieve this by nesting the `json_encode` function twice:

var jsonDataString = "{\"table1\":\"\\u63a8\\u5968\\u5207\\u524a\\u6761\\u4ef6 \\b\"}";
console.log(JSON.parse(jsonDataString));

Answer ā„–2

Give this a shot,

let sampleData ='{"table1":[{"Item":{"original_text":"\u63a8\u5968\u5207\u524a\u6761\u4ef6 \b"}}]}';

sampleData=sampleData.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");    

// Your code
// let jsonData = JSON.parse(sampleData);

let jsonData = JSON.parse(JSON.stringify(sampleData));

console.log(JSON.parse(JSON.stringify(jsonData)));

Answer ā„–3

please update the \b with an empty string: strData = strData.replace("\b","");

<script>
    var strData ='{"table1":[{"Item":{"original_text":"\u63a8\u5968\u5207\u524a\u6761\u4ef6 \b"}}]}';
   strData = strData.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t");   
   strData = strData.replace("\b","");
   document.write(strData); 
   var jsonData = JSON.parse(strData)
   document.write(jsonData);

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

Utilize useEffect to track a single property that relies on the values of several other properties

Below is a snippet of code: const MyComponent: React.FC<MyComponentProps> = ({ trackMyChanges, iChangeEverySecond }) => { // React Hook useEffect has missing dependencies: 'iChangeEverySecond' useEffect(() => { ...

Efficient Ways to Present and Personalize Indian Date and Time using JavaScript

Hey there, currently creating my website and need some assistance. I'm looking to display the date and time in different sections of the page. Having some trouble with getting Indian time to work properly using PHP. If you could help me out with Ja ...

AndroidStudio is obstinate when it comes to recognizing the hierarchy of inheritance

In my project, I am utilizing the library implementation 'org.json:json:20180813' I have developed a class called User which can be converted into a JSON string for data persistence purposes: data class User(override val name:String, override ...

Changing a single array into a series of arrays

I'm attempting to reverse the flattening process of an array. The JSON array I have as input contains 4 elements: [ { "nestedObj": { "id":12 } }, { "nestedObj": { "id":555 } ...

The issue of Angular 6 view failing to display accurate data upon page load

In my Angular 6 application, I've implemented a login feature using Firebase's Google login. The interface includes a button labeled Login when the user is logged out, and changes to Logout with the current email address displayed when the user i ...

Utilizing Mirth Connect to insert XML data into a MySQL database using JavaScript

I am completely new to working with Mirth, JavaScript, and MySQL. I have successfully set up a channel in Mirth to read a text file and convert it to XML. Everything is functioning properly so far. I also attempted to send the XML data to a MySQL databas ...

The extensive magnetic scrolling functionality in Ionic 2 sets it apart from other frameworks

Hi everyone, I could really use some assistance! I've been working on developing an Ionic 2 App and my navigation setup is not too complex. I have a main menu where clicking on an item opens another menu with a submenu. From there, if I click on an i ...

Pan motion gesture above HTML components

Is it possible to create a hovering effect over elements in a container using a pan gesture? Here's the HTML code: <div class="container"> <div class="item"></div> <div class="item"></div> <div class="item">< ...

A tutorial on utilizing a bundle in webpack based on certain conditions

My project consists of an app.bundle.js (the main app bundle) and two cordova bundles: iosCordova.bundle.js and androidCordova.bundle.js. Depending on whether the user is logging in on an iOS or Android device, I only script src one of them. All the bundle ...

When I attempt to run several promises simultaneously with Promise.All, I encounter an error

My code contains a series of promises, but they are not being executed as expected. Although the sequence is correct and functional, I have found that I need to utilize Promise.all in order for it to work properly. dataObj[0].pushScreen.map(item => { ...

Utilize a display value while maintaining the v-model binding in Vue.js

Can't seem to figure this one out. I'm using Quasar Framework, but it seems like more of a Vue issue. I have a multiple-select UI component with a list of objects as options that I want to display. The v-model will bind to the currently selected ...

Ember application experiencing trouble displaying Facebook Like Box

Iā€™m currently facing an issue with integrating the like box into our ember app, specifically in a template named about. The problem arises when users enter the ember app from a different route, instead of directly accessing the about route. In such cases ...

Forwarding users to a new destination through a script embedded within a frame

Currently, I am facing an issue where a page (lobby_box.php) is being loaded every 4 seconds on my main page (index.php) using JavaScript. The problem arises when the code within (lobby_box.php) that is meant to redirect the client from index.php to anothe ...

Samsung Galaxy S7 can interpret alphabetical parameters as numbers in a link sent via SMS

When trying to open a text message with a new message on my website using a link, I encountered an issue specifically with the Galaxy S7. The following code works on most Android phones: sms:5555555555?body=JOIN However, on the Galaxy S7, the "?body=JOIN ...

Getting JSON object from an HTTP callback in Retrofit 2.0: A guide

Greetings, I am currently utilizing Retrofit to retrieve a Java object. My goal is to update a textfield based on one of the fields in the response. However, I have been unable to locate any resources online that explain how to accomplish this task. Below ...

Jquery's remove function fails to function correctly when used on a cloned element

I am facing an issue where I have a list of rows that need to be deleted. However, when I attempted to use jQuery's remove function, it only removed the original row and not its clone. My goal is for the parent element of the parent to be removed when ...

The Javascript functionality does not seem to be functioning properly within the Bootstrap navigation

I'm having an issue with my Bootstrap navbar JavaScript. Below is the code that's causing the problem: <script type="text/javascript"> $(".nav a").on("click", function () { $(".nav").find(".active").removeClass("active"); $(this).p ...

Our system has picked up on the fact that your website is failing to verify reCAPTCHA solutions using Google reCAPTCHA V2

Error Message We have noticed that your website is not validating reCAPTCHA solutions. This validation is necessary for the correct functioning of reCAPTCHA on your site. Please refer to our developer site for further information. I have implemented the re ...

Jackson's @JsonAppend annotation with a predefined default value

Hey all! I'm currently in the process of building a web application and have chosen Jackson as my JSON processing framework. In terms of the request data I want to send, imagine my POJO structure like this: data class JSONEnvelope( @JsonProp ...

Is it possible to implement a goto-like functionality in Node.js or how can one define and invoke a function inside an asynchronous function?

Utilizing puppeteer, I have set up an automated test to fill out a form and verify captcha. In the event that the captcha is incorrect, the web page refreshes with a new image. However, this requires me to reprocess the new image in order to reach the func ...