Skip to content

Instantly share code, notes, and snippets.

@shadowlik
Created December 20, 2021 21:22
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shadowlik/50312b12804a0f697889d4742b7141d8 to your computer and use it in GitHub Desktop.
Save shadowlik/50312b12804a0f697889d4742b7141d8 to your computer and use it in GitHub Desktop.

Remove $decimal from express json response

Simple but effective way to remove recursively all the annoying $decimal key when using res.json.

It's a simple regex that cleans the response object as a json before sending.

const express = require('express');
const app = express();
app.use((req, res, next) => {
const { json } = res;
const fn: any = function (this: any, obj: any) {
const REGEXP = /{\s*"\$numberDecimal":\s*"(.+?)"\s*}/g;
/* eslint-disable-next-line */
obj = JSON.parse(JSON.stringify(obj).replace(REGEXP, '$1'));
json.call(this, obj);
};
res.json = fn;
next();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment