Created
January 21, 2023 19:10
-
-
Save Mr-Kumar-Abhishek/ce307e8bfd23181714f651551a34e3ed to your computer and use it in GitHub Desktop.
to create aplhanumberic ids in json object
This file contains 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
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