getServerSideProps cannot be serialized as JSON. Please only return JSON serializable data types
See original GitHub issueBug report
Describe the bug
I’m returning a row set from MySQL (using the mysql2 library) in my getServerSideProps. Getting the following error:
SerializableError: Error serializing .data[0]returned fromgetServerSidePropsin "/reports/[report]/[start]/[end]". Reason:object ("[object Object]") cannot be serialized as JSON. Please only return JSON serializable data types.
But calling JSON.stringify(data) works just fine.
If I do a JSON.parse(JSON.stringify(data)) it works just fine, but that seems very odd.
To Reproduce
- Execute a query using the
mysql2library - Return the row set as a prop in
getServerSideProps - Get Error.
Expected behavior
I expect the data to be serialized without an error since JSON.stringify() works just fine.
Additional context
Appears others are having this problem: https://stackoverflow.com/questions/61188494/getserversideprops-and-mysql-rowdatapacket
Issue Analytics
- State:
- Created 3 years ago
- Reactions:61
- Comments:31 (5 by maintainers)
Top Results From Across the Web
object` ("[object Response]") cannot be serialized as JSON?
getStaticProps and getServerSideProps only allow serializable content to be returned from them. To fix the issue you'll first need to ...
Read more >How to fix error serializing Date object JSON in Next.js
This is because you must return a JSON-serializable object. A Date object cannot be natively transformed into JSON.
Read more >I've spent the whole day on this, but no luck, I wanna die - Reddit
Reason: `object` ("[object Promise]") cannot be serialized as JSON. Please only return JSON serializable data types. Here is my function:
Read more >Dealing with Date objects in Next data fetching - James Perkins
Data fetching with dates in Next.js usually lead to a error serializing due to date timestamps. ... Please only return JSON serializable data...
Read more >error serializing `.csrftoken` returned from `getserversideprops`
Reason: object ("[object Object]") cannot be serialized as JSON. Please only return JSON serializable data types. But calling JSON.stringify(data) works ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

The main reasoning behind this limitation is that with
getInitialPropswe used to allow anything that can be stringified and that resulted in some really hard to track down UX bugs when hydrating client-side. Eg the hydration would fail because suddenly theDateobject you had server-side is a string client-side.We need to serialize the result of getStaticProps/getServerSideProps because it has to be sent to the browser to do hydration.
So we basically went with a strict checking mechanism (only runs in development) in order to prevent this from happening. We can potentially add an option to bypass in the future if there’s enough need for it 👍
@timneutkens I don’t get why this issue is closed. It seems quite a lot of people are still stumbling upon this and there is demand for a better solution. A warning in development mode seems to be sufficient to help users track down hydration errors and it wouldn’t require everyone to use hacks to serialize dates and such.