CodexBloom - Programming Q&A Platform

Difficulty Parsing Multi-line JSON Objects with Custom Quoting in Node.js

πŸ‘€ Views: 0 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-13
json node.js parsing JavaScript

I recently switched to I'm relatively new to this, so bear with me... I've been banging my head against this for hours. I'm currently working on a Node.js application where I'm trying to parse a multi-line JSON object that uses custom quoting for its keys and values, specifically single quotes instead of double quotes which is causing issues. Here's a sample of the JSON string I'm dealing with: ```json '{ 'name': 'John Doe', 'age': 30, 'address': { 'street': '123 Main St', 'city': 'Anytown' } }' ``` When I try to parse this using `JSON.parse()`, I get an behavior: `SyntaxError: Unexpected token ' in JSON at position 0`. I've tried replacing the single quotes with double quotes manually, but that becomes unmanageable for larger JSON objects. To handle this, I attempted to use a regular expression to replace single quotes with double quotes, but I encountered issues with nested quotes. Here's what I tried: ```javascript const jsonString = '{\n 'name': 'John Doe',\n 'age': 30,\n 'address': {\n 'street': '123 Main St',\n 'city': 'Anytown'\n }\n}'; const fixedJsonString = jsonString.replace(/'/g, '"'); const parsedData = JSON.parse(fixedJsonString); ``` This results in the behavior `SyntaxError: Unexpected token at position ...` at various points depending on the input. I’ve also looked into libraries like `json5`, but they still don’t seem to support my specific case of single quotes properly, especially for nested structures. Is there a recommended approach or library for handling this kind of parsing? Any advice on how to manage the potential nested structures while converting single quotes to double quotes efficiently would be greatly appreciated! I'm working on a API that needs to handle this. Has anyone else encountered this? I'm working in a Linux environment. Any suggestions would be helpful. The stack includes Javascript and several other technologies. Any feedback is welcome!