Changing a string into a JSON object using Javascript

I have a string in the following format:

var abc = "{'ABC':'25117', 'data':'India\"NewDelhi\"'}"

My goal is to convert this string into a JSON object.

I have attempted to interchange "" and '', but it did not work as expected.

When using the function JSON.parse(abc), it successfully works with the corrected string:

var abc = '{"ABC":"25117", "data":"India\'NewDelhi\'"}'

However, my strict requirement is to have "NewDelhi" within double quotes i.e. "".

Answer №1

Make sure to always use proper JSON formatting and remember to escape the quotes

var xyz = '{"XYZ":"12345", "location":"USA\\"NewYork\\""}';

FIDDLE

Answer №2

In order to properly parse your string, you must use escape characters like "\".

var xyz = "{\"XYZ\":\"90248\", \"info\":\"USA\\\"LosAngeles\\\"\"}"

After escaping the necessary characters, JSON.parse(xyz) will function correctly.

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 submission of the form is prevented upon updating the inner HTML

I am in the process of creating a website that will incorporate search, add, update, and delete functionalities all on a single webpage without any modals. The main focus of this webpage is facility maintenance. Adding facilities works smoothly; however, i ...

What modifications are needed to transform an input[type=text] element into a textarea?

Is there a way to make an input[type=text] element behave like a textarea, displaying multiple lines and having a larger height? Unfortunately, I cannot simply replace it with a textarea. Any suggestions on how to convert or modify the input element to w ...

How can I simultaneously execute two functions, one while the page is reloading/refreshing and the other on ng-click?

Here is the code snippet for the controller functions related to payment status. $scope.userhold_payment = function() { $http.get('http://localhost/ngaffiliate/api/payment/change_hold_payment_status') .then(function(response) { ...

Default Value for Null in Angular DataTable DTColumnBuilder

What is the best way to define a default value in case of null? $scope.dtOptions = DTOptionsBuilder .fromSource('api/Restt/List'); $scope.dtColumns = [ DTColumnBuilder.newColumn('modi ...

What is the best way to retrieve the date and time using the Open Weather API for a specific city

Currently, I am in the process of developing a weather application using React and integrating the Open Weather API. Additionally, I have incorporated an external library for weather icons. The functionality allows users to input a city name and receive t ...

Update the jQuery click event targeting the specific element

My knowledge of JavaScript and jQuery is limited. I recently purchased a WordPress theme and I am looking to make some customizations to it. On this specific page: https://websitesdevs.com/search-services/ There is a div element that displays the text "A ...

Maintaining Page State in React: Ensuring Users Stay on the Same Page After a Refresh

Incorporating react-router for navigating to different pages has been a smooth experience. However, upon refreshing the page, there is a brief redirection to the login page before returning to the homepage. This issue becomes more prominent when attempting ...

"Glitch in Yahoo Gifshot JavaScript causing issues with frame numbering

I have been using the Yahoo gifshot library to generate gifs from videos. A user uploads a video, and I use this library to dynamically create a gif from that video. However, the issue arises when a user uploads a two-second video but the resulting gif is ...

Utilize Polymer 3 component to import object values

In one of my polymer 3 components, I have declared an object property as follows: static get properties(): myInferface { return { myObject: { type: Object, notify: true, value: { showMe: false, text: ...

Checkbox remains selected even after navigating back

I am currently working on a code that involves using checkboxes. When I click on them, the checkbox value is appended to the URL with a hash. However, when I go back or press the back button, the URL changes but the checkboxes remain checked. Below is the ...

Create a BezierCurve dynamically using Three.js

I'm struggling to dynamically generate a BezierCurve from arrays in JavaScript. I tried using push method, but it's not working as expected. var x = [0,10,100,220,100, etc,...]; var y = [10,0,100,200,200, etc,...]; var z = [100,220,10,0,100, etc ...

Discover the proper way to implement dijit requires within a parsed Dojo ContentPane

I am currently faced with a challenge: loading an HTML file that contains dijit.Form into a ContentPane. The catch is, I am unsure of which dijits will be required until the HTML is actually loaded. However, I know that the loaded HTML will contain necessa ...

Sending JSON data to PHP using AJAX request

Currently attempting to send textbox values to a database via JSON and .getJSON. I am currently testing whether the JSON is successfully posting to the page, as I have confirmed that the UPDATE query is functioning correctly... The following code is not a ...

Transform svg into react-native-svg format

Can anyone recommend a straightforward method for achieving this? I have come across several svg to JSX converters, but unfortunately they do not work in react-native. I am looking for a way to convert the svg code into a format compatible with react-nat ...

Refresh the page twice using ajax/jQuery and setTimeout function

When it comes to Ajax, Java script or CSS, I must confess that I struggle a bit. So please bear with me as I ask for some assistance. I am currently working on a CMS wrapped in Jquery mobile and unable to use meta refresh methods. How can I adjust the code ...

Consistently encountering the error message "(0 , _reactDom.h) is not a function" or "h is not defined"

I'm currently in the process of developing an app that makes use of electron, react, redux, and several other technologies. At the moment, I have included electron, react, electron-compile, and babel in the project. Redux is installed but has not been ...

Implementing Google Maps on a webpage with a dynamic drop-down form using a combination of Javascript and PHP

On my HTML page, I have two drop-down lists populated with data from a MySQL database including latitude, longitude, and address. The user selects an item from the first drop-down and clicks submit. Upon submission, I want to display a Google map with a m ...

Setting the axios baseURL configuration globally in Nuxt.js

When starting a new project, you can use the following command: npm init nuxt-app <project-name> To set up the baseURL for axios, refer to this guide: npm install @nuxtjs/axios In your nuxt.config.js file: modules: [ '@nuxtjs/axios' ...

Error: The variable fullName has not been declared

To start off, I need to develop a registration form that includes fields for full name, email, password, mobile number, and date of birth. Once the email validation is successfully completed and the submit button is clicked, the user should be redirected t ...

When a form filled out using JavaScript is submitted, Wicket is receiving blank values

I am attempting to create a cross-selection feature using two multiselect elements. When I click a button, a simple JavaScript function transfers a value from one multiselect to another. This functionality works well, but when I try to submit the form, I o ...