JSON to Mongoose Schema Transformer — Numio
Infers a Mongoose schema definition from a sample JSON document and outputs it as JSON. Fields get String, Number, Boolean, Date, ObjectId or Mixed; nested objects become nested paths and arrays of objects subdocument schemas.
Writing a schema for an existing MongoDB collection from a document copied from Compass or mongosh.
JSON
Mongoose Schema
When to use it
- Writing a schema for an existing MongoDB collection from a document copied from Compass or mongosh.
- Starting a Node.js model from a sample API payload you plan to store.
How to use
- Paste a document as strict JSON, or an array of documents; write ObjectIds and dates as plain strings rather than ObjectId() or ISODate().
- Check the inferred types: 24-character hex strings become ObjectId and ISO 8601 strings such as 2024-05-01T08:00:00Z become Date.
- Copy the result into new mongoose.Schema({...}) and add required, default, index and ref options yourself.
JSON document
{
"_id": "64f1c2a9e1b2c3d4e5f60718",
"name": "Ada Lovelace",
"code": "A-1001",
"joinedAt": "2024-05-01T08:00:00Z",
"roles": ["admin", "editor"],
"addresses": [
{ "city": "London", "zip": "NW1" },
{ "city": "Paris", "zip": "75001", "primary": true }
],
"profile": { "bio": "Mathematician", "website": null }
}Mongoose schema (JSON)
{
"_id": {
"type": "ObjectId"
},
"name": {
"type": "String"
},
"code": {
"type": "String"
},
"joinedAt": {
"type": "Date"
},
"roles": {
"type": [
"String"
]
},
"addresses": {
"type": [
{
"city": {
"type": "String"
},
…Details and limitations
Inferred in the browser by the site's own generator; the document is not sent to a server.
Only ISO 8601 dates (2024-05-01, optionally with a time and offset) are typed Date, so codes such as "A-1001" or "12" stay String.
Arrays of objects become a subdocument schema built from all items, such as "type": [{ "city": ... }]. Empty arrays become Array and null becomes Mixed.
Required fields, defaults, indexes and refs cannot be inferred from data, and fields that are absent from your sample are missing from the schema.
FAQ
Can I convert a JSON array of documents?
Yes. The documents are merged, so every field that appears in any of them is in the schema. A field typed differently across documents becomes Mixed.
Why are the types quoted, like "String"?
The output is JSON, so type names are strings. In JavaScript you can use the global constructors String, Number, Boolean and Date and Schema.Types.ObjectId and Schema.Types.Mixed.
How is _id handled?
Like any other field: a 24-character hex string is typed ObjectId. Remove _id if you want Mongoose to add it automatically.