Strip quotes with JavaScript to convert into JSON object?


For this, you can use replace() along with parse(). Following is the code −

Example

var studentDetails = `"{""name"": ""John"",""subjectName"":
""Introduction To JavaScript""}"`;
console.log("The actual object=");
console.log(studentDetails);
var removeFirstAndLast = studentDetails.substring(1,studentDetails.length-1)
var removeDoubleQuotes = removeFirstAndLast.replace(/""/gi, `"`)
console.log(removeDoubleQuotes)
var output = JSON.parse(removeDoubleQuotes);
console.log(output);

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo103.js.

Output

This will produce the following output −

PS C:\Users\Amit\JavaScript-code> node demo103.js
The actual object=
"{""name"": ""John"",""subjectName"": ""Introduction To JavaScript""}"
{"name": "John","subjectName": "Introduction To JavaScript"}
{ name: 'John', subjectName: 'Introduction To JavaScript' }

Updated on: 07-Sep-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements