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

Add your comment