{"record":{"id":"e595e991e1e1b7b1","repo":"louislam/uptime-kuma","slug":"json-query-returned-the-array-truncatedresponse","errorCode":null,"errorMessage":"JSON query returned the array ${truncatedResponse}, but a primitive value is required. Modify your query to return a single value via [0] to get the first element or use an aggregation like $count(), $sum() or $boolean().","messagePattern":"JSON query returned the array (.+?), but a primitive value is required\\. Modify your query to return a single value via \\[0\\] to get the first element or use an aggregation like \\$count\\(\\), \\$sum\\(\\) or \\$boolean\\(\\)\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"warning","filePath":"src/util.ts","lineNumber":717,"sourceCode":"        response = JSON.parse(data);\n    } catch {\n        response =\n            (typeof data === \"object\" || typeof data === \"number\") && !Buffer.isBuffer(data) ? data : data.toString();\n    }\n\n    try {\n        // If a JSON path is provided, pre-evaluate the data using it.\n        response = jsonPath ? await jsonata(jsonPath).evaluate(response) : response;\n\n        if (response === null || response === undefined) {\n            throw new Error(\"Empty or undefined response. Check query syntax and response structure\");\n        }\n\n        // Check for arrays: JSONata filter expressions like .[predicate] always return arrays\n        if (Array.isArray(response)) {\n            const responseStr = JSON.stringify(response);\n            const truncatedResponse = responseStr.length > 25 ? responseStr.substring(0, 25) + \"...]\" : responseStr;\n            throw new Error(\n                \"JSON query returned the array \" +\n                    truncatedResponse +\n                    \", but a primitive value is required. \" +\n                    \"Modify your query to return a single value via [0] to get the first element or use an aggregation like $count(), $sum() or $boolean().\"\n            );\n        }\n\n        if (typeof response === \"object\" || response instanceof Date || typeof response === \"function\") {\n            throw new Error(\n                `The post-JSON query evaluated response from the server is of type ${typeof response}, which cannot be directly compared to the expected value`\n            );\n        }\n\n        // Perform the comparison logic using the chosen operator\n        let jsonQueryExpression;\n        switch (jsonPathOperator) {\n            case \">\":\n            case \">=\":","sourceCodeStart":699,"sourceCodeEnd":735,"githubUrl":"https://github.com/louislam/uptime-kuma/blob/6b5ea0155793e666666745fb8d6fef1e829543a2/src/util.ts#L699-L735","documentation":"Thrown by evaluateJsonQuery() when the post-JSONata response is an Array. The json-query monitor can only compare a single primitive value against the expected value, so an array result is rejected with guidance to project to one element or aggregate. The message embeds a truncated (<=25 chars) JSON of the array to aid debugging.","triggerScenarios":"monitor.jsonPath uses a wildcard or filter that yields multiple values: `$.users[*].name`, `$.items[type='book']`, `$..version` matching several nodes, `$.metrics[*]`. Any path returning more than one element lands here.","commonSituations":"User writes a JSONata filter expression expecting one match but several match; target payload shape changed from single object to array; predicate is too broad; user did not know wildcards return arrays.","solutions":["Project to a single element: append `[0]` (or `[last]`) to the path to take the first/last match.","Use a JSONata aggregation: `$count($.items[type='book'])`, `$sum($.orders[*].price)`, `$boolean($.items[type='book'])`."],"exampleFix":"// before: monitor.jsonPath = \"$.items[type='book']\" -> array -> throws\n\n// after: pick the first match\n//   monitor.jsonPath = \"$.items[type='book'][0]\"\n// or aggregate:\n//   monitor.jsonPath = \"$count($.items[type='book'])\"","handlingStrategy":"validation","validationCode":"const probe = await jsonata(jsonPath).evaluate(sample);\nif (Array.isArray(probe)) throw new Error(\"Path returns an array; add [0] or an aggregation\");","typeGuard":"const isPrimitive = (v) => v === null || v === undefined || [\"string\",\"number\",\"boolean\"].includes(typeof v);","tryCatchPattern":"try { await evaluateJsonQuery(data, path, op, expected); } catch (e) { if (/returned the array/.test(e.message)) { path += \"[0]\"; /* retry */ } else throw e; }","preventionTips":["Append [0] to filter/wildcard paths that should yield one element.","Use $count/$sum/$boolean when you need an aggregate.","Inspect the response shape before writing the path."],"tags":["json-query","jsonata","monitor","validation","typescript","uptime-kuma"],"backgroundTag":null,"analyzedSha":"6b5ea0155793e666666745fb8d6fef1e829543a2","analyzedAt":"2026-08-12T23:42:12.959Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}