How JSON.stringify Corrupted 150,000 Rows in My Postgres Database
📰 Hackernoon
Learn how JSON.stringify corrupted 150,000 rows in a Postgres database and how to safely store JSON data using sql.json
Action Steps
- Use jsonb_typeof to detect corrupted jsonb strings
- Run a query to identify rows with corrupted data
- Apply a repair function to convert jsonb strings back to arrays
- Configure your database driver to use sql.json instead of JSON.stringify
- Test your data storage and retrieval process to ensure data integrity
Who Needs to Know This
Backend developers and database administrators can benefit from this lesson to avoid similar data corruption issues in their own projects
Key Insight
💡 Using JSON.stringify with postgres.js can lead to double serialization, causing data corruption
Share This
🚨 JSON.stringify can silently corrupt your Postgres data! 🚨 Learn how to detect and repair corrupted jsonb strings
Key Takeaways
Learn how JSON.stringify corrupted 150,000 rows in a Postgres database and how to safely store JSON data using sql.json
Full Article
I stored arrays in Postgres jsonb columns using postgres.js + JSON.stringify. The driver serialized them a second time, so ~150,000 values across ten columns were silently saved as jsonb strings, not arrays. They looked right and matched no filters. Root cause, detection (jsonb_typeof = 'string'), repair, and the safe pattern (sql.json).
DeepCamp AI