louislam/uptime-kuma · error · Error
Invalid JSON in database query: ${error.message}
Error message
Invalid JSON in database query: ${error.message} What it means
Thrown by Monitor.validate() for type "mongodb" when databaseQuery is set and JSON.parse fails. The query is stored as a JSON string and later parsed by the MongoDB monitor to build a find/aggregation call, so malformed JSON would crash at check time.
Source
Thrown at server/model/monitor.js:1777
const maxDelayFromTimeout = this.interval * 1000 * 0.8;
if (delay >= maxDelayFromTimeout) {
throw new Error(`Screenshot delay must be less than ${maxDelayFromTimeout}ms (0.8 × interval)`);
}
// Must not exceed 0.5 * interval to prevent blocking next check
const maxDelayFromInterval = this.interval * 1000 * 0.5;
if (delay >= maxDelayFromInterval) {
throw new Error(`Screenshot delay must be less than ${maxDelayFromInterval}ms (0.5 × interval)`);
}
}
}
if (this.type === "mongodb" && this.databaseQuery) {
// Validate that databaseQuery is valid JSON
try {
JSON.parse(this.databaseQuery);
} catch (error) {
throw new Error(`Invalid JSON in database query: ${error.message}`);
}
}
}
/**
* Gets monitor notification of multiple monitor
* @param {Array} monitorIDs IDs of monitor to get
* @returns {Promise<LooseObject<any>>} object
*/
static async getMonitorNotification(monitorIDs) {
return await R.getAll(
`
SELECT monitor_notification.monitor_id, monitor_notification.notification_id
FROM monitor_notification
WHERE monitor_notification.monitor_id IN (${monitorIDs.map((_) => "?").join(",")})
`,
monitorIDs
);View on GitHub (pinned to 6b5ea01557)
Solutions
- Write strict JSON: double-quoted keys, no trailing commas, no comments, e.g. "{ "limit": 1, "sort": { "_id": -1 } }".
- Validate the string with a JSON linter or JSON.parse before submitting.
- If you need shell-only syntax, convert it to JSON first using a tool such as `mongo --eval` to canonicalize.
Example fix
// before
{ type: "mongodb", databaseQuery: "{ limit: 1, }" }
// after
{ type: "mongodb", databaseQuery: "{ \"limit\": 1 }" } Defensive patterns
Strategy: validation
Validate before calling
function validMongoQuery(q) {
if (!q) return true;
try { JSON.parse(q); return true; } catch { return false; }
} Type guard
function isJsonString(v) {
if (typeof v !== "string" || v.length === 0) return v == null;
try { JSON.parse(v); return true; } catch { return false; }
} Try / catch
try {
await bean.validate();
} catch (e) {
if (/Invalid JSON in database query/.test(e.message)) return badRequest("databaseQuery must be strict JSON");
throw e;
} Prevention
- Run the query through JSON.parse in a scratch pad before saving.
- Prefer building the query with JSON.stringify(obj) so quoting is automatic.
When it happens
Trigger: Save a mongodb monitor with databaseQuery containing trailing commas, single quotes, unquoted keys, comments, or unescaped characters. e.g. "{ limit: 1, }" or "{'field': 1}".
Common situations: User pastes a Mongo shell expression (JS object literal) instead of strict JSON. Editing the query in a text editor that auto-inserts smart quotes. Copying from MongoDB Compass which may emit shell syntax.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Service Name is required.
- Invalid service name. Please use the internal Service Name (
- Invalid PM2 process name.
- Packet size must be between ${PING_PACKET_SIZE_MIN} and ${PI
- Per-ping timeout must be between ${PING_PER_REQUEST_TIMEOUT_
AI-assisted analysis of louislam/uptime-kuma@6b5ea01557 (2026-08-12).
Data as JSON: /api/errors/8f823f8c3199b644.
Report an issue: GitHub.