question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSON.stringify is converting `Float` (`number`) values to strings.

See original GitHub issue

Bug description

I’m using prisma with Typescript in a next.js application and JSON.strinfigy converts fields that are of type number (Float in the schema) to strings.

I did a little debugging and it looks like the Float fields are actually typeof 'object', not number, as I would have expected and what the generated @prisma/client types are telling me. The only thing I can think of is that Prisma is wrapping these values in something funky in the background?

For now my workaround is to do something like this:

export const toRaw = (user: User) => ({
  id: user.id,
  ...
  name: user.name,
  latitude: Number(user.latitude),
  longitude: Number(user.longitude),
});

How to reproduce

When I fetch and try to return json, latitude and longitude are being converted to strings:

const user = await prisma.user.findUnique({
  where: {
    id: req.user.id,
  },
});

JSON.stringify(user);

The output of which is:

{
  "name": "Super Dude",
  "longitude": "-118.235322", // <- STRING, not a number
  "latitude": "34.045148",  // <- STRING, not a number
}

Expected behavior

I’d expect the output to be:

{
  "name": "Super Dude",
  "longitude": -118.235322, // <- NUMBER, not a string
  "latitude": 34.045148,  // <- NUMBER, not a string
}

Prisma information

My schema for a User model is as follows:

// Schema
model User {
  id            String      @id @default(cuid())
  ...
  name          String?
  latitude      Float?
  longitude     Float?
}

// Generated type in @prisma/client
/**
 * Model User
 * 
 */
export type User = {
  id: string
  ...
  name: string | null
  latitude: number | null
  longitude: number | null
}

Environment & setup

Next.js with Typescript.

Prisma Version

prisma                  : 3.10.0
@prisma/client          : 3.10.0
Current platform        : debian-openssl-1.1.x
Query Engine (Node-API) : libquery-engine 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/libquery_engine-debian-openssl-1.1.x.so.node)
Migration Engine        : migration-engine-cli 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/migration-engine-debian-openssl-1.1.x)
Introspection Engine    : introspection-core 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/introspection-engine-debian-openssl-1.1.x)
Format Binary           : prisma-fmt 73e60b76d394f8d37d8ebd1f8918c79029f0db86 (at node_modules/@prisma/engines/prisma-fmt-debian-openssl-1.1.x)
Default Engines Hash    : 73e60b76d394f8d37d8ebd1f8918c79029f0db86
Studio                  : 0.458.0

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ifightcrimecommented, Sep 9, 2022

@janpio hey yes sorry I missed the first message! At the time I was a number of versions back too so who knows. I’m about to head out for the weekend though, but I will get back to you likely early next week.

1reaction
pantharshit00commented, Mar 22, 2022

I am unable to reproduce this. I ran the query that you have posted and ran JSON.stringify to the result and output was correct: image

I used Postgres as database? Can you please try again with latest version and possibly report which database are you using?

Edit: Tried again with the same values as you have specified and still got the correct result: image

Script used:

import { PrismaClient } from "@prisma/client";

async function main() {
  const prisma = new PrismaClient();

  const user = await prisma.user.findUnique({
    where: {
      id: "cl11yqszv00072311gdwx6vo7",
    },
  });

  console.log(JSON.stringify(user));
  await prisma.$disconnect();
}

main();
Read more comments on GitHub >

github_iconTop Results From Across the Web

Force float value when using JSON.stringify - Stack Overflow
To be clear We are talking about conversion number to string not number to float ( such type not exists in javascript )...
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or ...
Read more >
stringify-with-floats - npm
Start using stringify-with-floats in your project by running `npm i stringify-with-floats`. ... value The value to convert to a JSON string ...
Read more >
How to convert float values in JSON to two decimal place ...
You could pass reviver function to the JSON.parse method to prescribes how the value originally produced by parsing is transformed, before being returned:...
Read more >
JSON.stringify makes bigints and decimals to strings
Since javascript treats JSON numbers as double-precision floats, it can't represent all CQL bigint and decimal values as javascript Numbers. When converting ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found