Strip quotes with JavaScript to convert into JSON object?

JavascriptWeb DevelopmentObject Oriented Programming

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' }
raja
Published on 07-Sep-2020 12:19:30
Advertisements