Registry / database / json2mongo

json2mongo

JSON →
library2.0.0jsnpmunverified

Converts MongoDB Extended JSON Strict Mode syntax to JavaScript Mode, allowing use of Date, RegExp, and undefined in MongoDB query objects directly. Version 2.0.0 is the latest stable release, updated infrequently. Unlike manual parsing, it handles nested objects and all extended JSON types ($date, $regex, $undefined) automatically, converting to native JavaScript Date, RegExp, and undefined values.

npm install json2mongo
INSTALL
IMPORT
SIG · JSON2MONGO
J
json2mongo
databasejavascriptv2.0.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
import json2mongo from 'json2mongo'
const json2mongo = require('json2mongo')
ESM import is recommended for modern projects; CommonJS require still works for Node.js.
default (CommonJS)
const json2mongo = require('json2mongo')
import json2mongo from 'json2mongo'; // if package.json lacks type: module
Package does not specify type: module, so CommonJS require is the default in Node.js.
default (TypeScript)
import json2mongo from 'json2mongo'
import * as json2mongo from 'json2mongo'; // unnecessary, use default import
No TypeScript types are shipped; you may need to install @types/json2mongo or declare module.

Converts MongoDB Extended JSON Strict Mode with $date, $undefined, $regex to native JavaScript types.

import json2mongo from 'json2mongo'; const query = { created: { $date: '2023-01-01T00:00:00.000Z' }, foo: { $undefined: true }, bar: { $regex: '[0-9]' }, baz: { $regex: '[a-z]', $options: 'i' } }; const result = json2mongo(query); console.log(result); // { // created: new Date('2023-01-01T00:00:00.000Z'), // foo: undefined, // bar: /[0-9]/, // baz: /[a-z]/i // }
Debug
Known issues
gotchaPackage lacks TypeScript type definitions; you must supply a .d.ts file or use @types/json2mongo if available.
fix
Add declare module 'json2mongo' in a .d.ts file or install @types/json2mongo.
affects: >=0.0.0
gotchaThe function mutates the input object in place and also returns it; avoid reusing the original object after conversion.
fix
Clone the input before passing if the original is needed: const cloned = JSON.parse(JSON.stringify(original));
affects: >=2.0.0
gotcha$undefined: true converts to undefined, which removes the key when serialized to JSON (JSON.stringify removes keys with undefined values).
fix
If the property must persist, consider omitting $undefined or post-process the result.
affects: >=0.0.0
Errors
Common errors & fixes
TypeError: json2mongo is not a function
Using ES6 import with a CommonJS module that doesn't have a default export semantics properly set up.
fix
Use const json2mongo = require('json2mongo') or enable esModuleInterop in TypeScript.
Cannot find module 'json2mongo' or its corresponding type declarations.
TypeScript cannot resolve the module type; no declaration file exists.
fix
Create a global .d.ts file with declare module 'json2mongo'; or use require style.
Upgrade
Version history
2.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
5 hits · last 30 days
node
4
Amazon
1
Resources
json2mongo — npm install json2mongo · libregistry