diff --git a/utils/sortJSON.js b/utils/sortJSON.js index 2c86dcf..b5829d6 100644 --- a/utils/sortJSON.js +++ b/utils/sortJSON.js @@ -31,13 +31,18 @@ fs.readFile(filepath, 'utf-8', (err, data) => { jsonData.sort((a, b) => a.title.localeCompare(b.title)); // Write the sorted and amended JSON data back to the file - fs.writeFile(filepath, JSON.stringify(jsonData, null, 2), (err) => { - if (err) { - console.error(`Error writing file: ${err.message}`); - return; - } - console.log(`File "${filepath}" sorted and structured successfully.`); - }); + try { + const jsonString = JSON.stringify(jsonData, null, 2); + fs.writeFile(filepath, jsonString, (err) => { + if (err) { + console.error(`Error writing file: ${err.message}`); + return; + } + console.log(`File "${filepath}" sorted and structured successfully.`); + }); + } catch (error) { + console.error(`Error stringifying JSON data: ${error.message}`); + } } catch (error) { console.error(`Error parsing JSON data: ${error.message}`); }