Skip to content

Instantly share code, notes, and snippets.

@anataliocs
Created November 24, 2025 17:52
Show Gist options
  • Select an option

  • Save anataliocs/3fbf2ee9ed56108ed0980256fcac0291 to your computer and use it in GitHub Desktop.

Select an option

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
// 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',
);
@anataliocs
Copy link
Author

anataliocs commented Nov 24, 2025

Usage

Update file name
Update line 7 const filePath = path.join(__dirname, 'config.json');

Replace 'config.json' with the name of your file

Execute:

node quote-json-keys.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment