/**
* Hashes JSON responses for quick error identification.
* @param {object} json The JSON response to hash.
* @returns {string} A hash string representing the JSON, or an error message if hashing fails.
*/
function hashJson(json) {
try {
const jsonString = JSON.stringify(json, null, 2); // Stringify with indentation for readability
const hash = crypto.createHash('sha256').update(jsonString).digest('hex'); // Use SHA256 for a strong hash
return hash;
} catch (error) {
return `Error hashing JSON: ${error.message}`; // Handle potential errors during JSON processing
}
}
// Export the function (optional, for modularity)
module.exports = hashJson;
//Example usage (requires crypto module)
// const crypto = require('crypto'); // Import the crypto module
// const myJson = { name: "John", age: 30, city: "New York" };
// const hashValue = hashJson(myJson);
// console.log(hashValue);
Add your comment