Double quotes in JSON are not being escaped properly

When I try to evaluate some json on a screen, I keep encountering errors with the message 'unexpected identifier'...

The problematic data that seems to be causing this issue is:

"itemDescription":"STANDARD \"B\" RED BOX",

To address this, I have tried using the following java code snippet to handle double quotes:

itemDescription = itemDescription.replaceAll("\\r|\\n", "");
itemDescription = itemDescription.replaceAll("\"", "\\\\\"");
itemDescription = itemDescription.replaceAll("'", "'");

However, despite these efforts, the issue persists. Interestingly, if I remove the double quotes altogether, the errors vanish.

In contrast, item descriptions like "itemDescription":"STANDARD 16\" RED BOX" seem to process without any issues...

Any insights or suggestions would be greatly appreciated! Thanks!

Answer №1

Ensure to include two additional backslashes on line 2:

itemDescription = itemDescription.replaceAll("\"", "\\\\\\\"").

This will replace " with \\\" instead of \\".

Answer №2

Consider utilizing the StringEscapeUtils library :

sanitizedDescription = StringEscapeUtils.escapeJson(itemDescription);

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

The TypeError thrown by Mongo .updateMany() indicates that the property 'updateMany' of the object is not a valid function

Currently, I have a collection named users, which contains the following documents: Document 1: { "_id": { "$oid": "5934fd84d6ba4c241259bed1" }, "first_name": "Joe", "last_name": "Smith", "username": "jsmith", "email": "&l ...

Navigating the From and To routes in Nuxt/Vue: A comprehensive guide

I am currently working on a Nuxt/Vue project. While inspecting in Dev Tools, I came across a From and To property. How can I access these properties within the Nuxt application? I have attempted to use this.$nuxt.$route, this.$nuxt.$router, and this.$rou ...

Can someone please explain how to use the prevState in REACT?

Can you explain the difference between how to define the counterHandler function in these two examples? counterHandler = () => { this.setState(() => { return { times: this.state.times + 1 } }); } versus counterHandle ...

Monitoring Object Changes in Angular 4

ETA: I am aware of different methods for monitoring my form for alterations. However, that is not the focus of my inquiry. As indicated by the title, my question pertains to observing changes within an object. The application displayed below is solely for ...

Encode a MySQL query using json_encode to generate data for a multi-series flot chart

I'm attempting to convert a MySQL query into the specified JSON format for a flot chart, as outlined in the documentation: [ { label: "Foo", data: [ [10, 1], [17, -14], [30, 5] ] }, { label: "Bar", data: [ [11, 13], [19, 11], [30, -7] ] } ] Here i ...

Error: NullPointerException encountered while executing the put method in a HashMap

Despite this question being asked numerous times before, I am still struggling to comprehend my mistake. The code I have written is a standard method for counting the number of repetitions in an array. However, I acknowledge that it may be too lengthy. Pl ...

reconfigure form credentials with JavaScript

I am currently working on a form that includes a textbox and a button for submitting data using ajax. <input type="password" id="password" /> <button id="addaccount" onclick="showload();">Add</button> When the user clicks on the button, ...

Script for uploading multiple images without using flash, with customization options available for each individual upload

Looking for a non-flash images uploader script that has the following features: Ability to upload multiple files Supports drag and drop functionality Displays progress bar for each upload Shows small preview of each upload Allows for resumable downloads ...

exchanging the positions of two animated flipdivs

Hey there! I'm facing a little challenge with my two divs (let's call them div1 and div2). One of them has a flip animation on hover, while the other flips on toggle click. My goal is to swap these two divs by dragging and dropping them. I' ...

Can objects be Serialized and Deserialized in C++?

Is C++ also an Object-Oriented Programming language like Java, where most things are objects? I am curious to know if the Serialize and Deserialize features are available in C++ as they are in Java. If so, how can it be accomplished? In Java, we use the ...

Having difficulties with the edit function in JavaScript To Do Application

After creating an edit button to modify a task, it functions properly for the initial task I create. However, when attempting to edit subsequent tasks, the edit function defaults back to modifying the input of the first task instead. With each new ta ...

Surprising outcomes when using MongooseJS's findOne() method

Currently utilizing Mongoose as the Object Document Mapper (ODM) alongside NodeJS, but struggling with comprehending how error handling functions. It seems to be working, however the implementation appears incorrect and does not align with the documentatio ...

Is there a way to allow only the block code to shift while keeping the other span tags stationary?

Is it possible to change the text without affecting other span tags in the code? I want to make sure only this specific text is updated. How can I achieve that? var para_values = [ { content: "BRAND " }, { content: "MISSION" } ]; functi ...

How to utilize local functions within a ko.computed expression

Why isn't this line of code working? I'm using durandal/knockout and my structure is like this define(function () { var vm = function() { compute: ko.computed(function() { return _compute(1); // encountering errors }); ...

How can I efficiently add multiple items to an array and store them in async storage using React Native?

I am trying to store multiple elements in local storage using React Native. I found some helpful documentation on how to do this here. Could someone guide me on the correct way to achieve this? Here's a snippet of my code: My current approach const ...

The onEnter and onExit functions within a React Native Component using react-native-router-flux

Within my app's router definitions, I have successfully utilized the onEnter/onExit method at the root level: <Scene key="arena" hideNavBar={true} onEnter={() => console.log("Entered")} component={ArenaPage} /> Is there a way to achieve th ...

When the browser is not in the foreground, clicking on the Bootstrap datepicker with Selenium does not register

When you click on the input field <input id="dp1" class="span2" type="text" value="02-16-2012"> If the browser is in the background, the datepicker popup will not display. Even using javascript or jquery to click the input field does not show the ...

An error was encountered in the index.js file within the app directory when running the webpack

Recently, I was told to learn react.js even though my knowledge of javascript is limited. Nevertheless, I decided to dive in and start with a simple "Hello World" project. Initially, when I used the index.js file below and ran webpack -p, everything worke ...

The UI-Grid feature in Angular, when set to right-to-left (RTL) mode, incorrectly displays data in the opposite order compared to the

In my _Layout.cshtml file, I have a CSS that sets all UI controls to right-to-left direction using the following code: * { direction: rtl; } Currently, I am working with Angular's UI-Grid, and in RTL Localization, the ...

How do I prevent a specific word from being removed in a contenteditable div using JavaScript?

Attempting to create a terminal-like experience in JS, I am looking to generate the word 'current source, current location' (e.g., admin@ubuntuTLS~$: ~/Desktop) at the beginning which cannot be removed. Also, I want to prevent the caret from bein ...