Created
November 24, 2025 17:52
-
-
Save anataliocs/3fbf2ee9ed56108ed0980256fcac0291 to your computer and use it in GitHub Desktop.
When you console.log() a JSON config, the object keys may not be quoted. This script will take in a file and wrap all the object keys in double quotes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Utility script: Wrap all unquoted parameter names (object keys) with double quotes | |
| // Usage: quote-json-keys.js | |
| const fs = require('fs'); | |
| const path = require('path'); | |
| const filePath = path.join(__dirname, 'config.json'); | |
| let text = fs.readFileSync(filePath, 'utf8'); | |
| // Replace unquoted keys at start of line up to colon with quoted keys. | |
| // It intentionally ignores keys already quoted. | |
| text = text.replace(/^(\s*)(?!\")(\w[\w-]*)(\s*):/gm, '$1"$2"$3:'); | |
| fs.writeFileSync(filePath, text, 'utf8'); | |
| console.log( | |
| 'Wrapped all unquoted parameter names with double quotes in config.json', | |
| ); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Update file name
Update line 7
const filePath = path.join(__dirname, 'config.json');Replace
'config.json'with the name of your fileExecute: