How to handle serialization in Prisma (TypeError: Do not know how to serialize a BigInt)
Hi everyone 👋
I’m encountering an issue when making a query in my app. The request fails with the following error:
TypeError: Do not know how to serialize a BigInt
After digging in, it seems that some values in my Prisma response include BigInt, which causes JSON.stringify() to fail. I've implemented the following serializer in order to conver these values from BigInt into string:
// Custom JSON serializer to handle BigInt values
const customJsonSerializer = (data: any) => {
return JSON.stringify(data, (_, value) =>
typeof value === "bigint" ? value.toString() : value
);
};
However, I wonder: Is there anything built into Prisma (or recommended by the team) to help convert BigInt into a JSON-safe format like string? Or is writing a custom serializer the best approach?
Thanks in advance for your help!
2 Replies
Well met, adventurer! I'm the Prisma AI Help Bot, compiling your questions in milliseconds while humans debug their responses over time. Who’s on your team?
Yup! This looks to be a good solution. You can see a lot of discussion and other approaches here: https://github.com/prisma/studio/issues/614
mdn also has a really good section on this: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt#use_within_json