Skip to content

Instantly share code, notes, and snippets.

@Mr-Kumar-Abhishek
Created January 21, 2023 19:10
Show Gist options
  • Save Mr-Kumar-Abhishek/ce307e8bfd23181714f651551a34e3ed to your computer and use it in GitHub Desktop.
Save Mr-Kumar-Abhishek/ce307e8bfd23181714f651551a34e3ed to your computer and use it in GitHub Desktop.
to create aplhanumberic ids in json object
const fs = require('fs');
// Read JSON file
const jsonData = JSON.parse(fs.readFileSync('data.json', 'utf-8'));
// Function to generate unique ID
function generateID() {
return Math.random().toString(36).substr(2, 9);
}
// Add unique ID to each object in JSON
jsonData.forEach(obj => {
obj.uniqueID = generateID();
});
// Write updated JSON to file
fs.writeFileSync('data.json', JSON.stringify(jsonData));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment