Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Strip quotes with JavaScript to convert into JSON object?
When dealing with JSON strings that have escaped quotes or extra quote wrapping, you need to clean them before parsing. This article shows how to strip unwanted quotes and convert the cleaned string into a JavaScript object.
The Problem
Sometimes JSON data comes with extra quotes or escaped quote characters that prevent direct parsing. Here's a common scenario where a JSON string is wrapped in extra quotes and has doubled internal quotes:
var studentDetails = `"{""name"": ""John"",""subjectName"": ""Introduction To JavaScript""}"`;
console.log("Original string:");
console.log(studentDetails);
Original string:
"{""name"": ""John"",""subjectName"": ""Introduction To JavaScript""}"
Solution: Strip and Parse
The solution involves two steps: removing the outer quotes using substring() and replacing double quotes with single quotes using replace():
var studentDetails = `"{""name"": ""John"",""subjectName"": ""Introduction To JavaScript""}"`;
// Step 1: Remove first and last quote characters
var removeFirstAndLast = studentDetails.substring(1, studentDetails.length - 1);
console.log("After removing outer quotes:");
console.log(removeFirstAndLast);
// Step 2: Replace double quotes with single quotes
var removeDoubleQuotes = removeFirstAndLast.replace(/""/gi, `"`);
console.log("After fixing double quotes:");
console.log(removeDoubleQuotes);
// Step 3: Parse into JavaScript object
var output = JSON.parse(removeDoubleQuotes);
console.log("Final JavaScript object:");
console.log(output);
After removing outer quotes:
{""name"": ""John"",""subjectName"": ""Introduction To JavaScript""}
After fixing double quotes:
{"name": "John","subjectName": "Introduction To JavaScript"}
Final JavaScript object:
{ name: 'John', subjectName: 'Introduction To JavaScript' }
How It Works
-
substring(1, length-1)removes the first and last characters (outer quotes) -
replace(/""/gi, '"')uses a global case-insensitive regex to replace all double quotes with single quotes -
JSON.parse()converts the cleaned string into a JavaScript object
Alternative Approach
For more complex quote stripping, you can combine both operations:
var studentDetails = `"{""name"": ""John"",""subjectName"": ""Introduction To JavaScript""}"`;
// One-liner approach
var cleanedString = studentDetails.substring(1, studentDetails.length - 1).replace(/""/g, '"');
var result = JSON.parse(cleanedString);
console.log("Result:", result);
console.log("Name:", result.name);
console.log("Subject:", result.subjectName);
Result: { name: 'John', subjectName: 'Introduction To JavaScript' }
Name: John
Subject: Introduction To JavaScript
Conclusion
Use substring() to remove outer quotes and replace() with regex to fix internal quote escaping. Always validate your JSON string format before parsing to avoid errors.
