JSONata Query Issue In Uptime Kuma: Evaluation Error
In this article, we'll dive into a peculiar issue encountered while using Uptime Kuma, a widely-used monitoring tool, specifically with JSONata query evaluations. We'll explore a scenario where the evaluation of a JSONata query within an HTTP(s) JSON Query monitor yields an unexpected result, leading to potential monitoring inaccuracies. Let's unravel this issue and understand its implications.
Understanding the Issue
The core of the problem lies in the discrepancy between the expected and actual results of a JSONata query within Uptime Kuma's monitoring system. JSONata, a powerful JSON query and transformation language, is used to extract and manipulate data from JSON payloads. In this specific case, a user reported that a JSONata query designed to count the number of elements in a JSON array was returning an incorrect count.
The Scenario
The user configured an HTTP(s) JSON Query monitor in Uptime Kuma version 2.0.1. The monitor was set to observe a web service that returns a JSON payload containing an array of orders. The goal was to trigger an alert if the number of orders was zero. To achieve this, the user employed the following JSONata query expression:
$count(orders) == 0
This expression intends to count the number of elements in the orders array and compare it to zero. If the count is zero, the expression should evaluate to true; otherwise, it should evaluate to false.
The Payload
The web service being monitored returns the following JSON payload:
{
"orders": [
{
"batch": "love",
"date": "2025-11-01 15:15:43",
"kind": "STOCK_IN",
"line": "LINE_1",
"reasons": []
},
{
"batch": "lace",
"date": "2025-11-01 15:15:14",
"kind": "STOCK_OUT",
"line": "LINE_2",
"reasons": []
}
]
}
As you can see, the orders array contains two elements. Therefore, the expected result of the $count(orders) expression should be 2.
The Discrepancy
However, Uptime Kuma's monitor reported the evaluation message as Json query passes ( comparing 0 == 0). This indicates that Uptime Kuma evaluated $count(orders) as 0, which is incorrect. The user verified this discrepancy by testing the same JSONata expression against the same payload in the online JSONata Exerciser, which correctly returned 2.
This inconsistency raises concerns about the reliability of JSONata query evaluations within Uptime Kuma and necessitates a deeper investigation to identify the root cause.
Reproduction Steps
To reproduce this issue, follow these steps:
- Set up an HTTP(s) JSON Query monitor in Uptime Kuma.
- Configure the monitor to target a web service that returns the JSON payload provided above.
- Specify the JSONata query expression as
$count(orders) == 0. - Observe the monitor's evaluation result. You should see that the query incorrectly evaluates to
true(or 0).
Expected Behavior
The expected behavior is that the JSONata query $count(orders) should correctly evaluate to 2, reflecting the number of elements in the orders array. Consequently, the expression $count(orders) == 0 should evaluate to false.
Actual Behavior
In reality, the JSONata query $count(orders) is incorrectly evaluated as 0 within Uptime Kuma, leading the expression $count(orders) == 0 to evaluate to true. This discrepancy can lead to false negatives in monitoring, where alerts are not triggered when they should be.
Technical Details
Uptime Kuma Version
The issue was observed in Uptime Kuma version 2.0.1.
Operating System and Architecture
- Operating System: Debian GNU/Linux 13 x86_64
Browser
- Browser: FireFox
Deployment Environment
- Runtime Environment:
- Node.js: Version
v22.21.0
- Node.js: Version
- Database:
- SQLite: Embedded
- Database Storage:
- Filesystem:
- Linux: ext4
- Storage Medium: NVMe
- Filesystem:
- Uptime Kuma Setup:
- Number of monitors:
10
- Number of monitors:
Potential Causes and Solutions
Several factors could contribute to this issue. Let's explore some potential causes and corresponding solutions:
-
JSONata Implementation Bug: There might be a bug in the specific JSONata implementation used by Uptime Kuma. Different JSONata libraries or versions might have subtle variations in their behavior. If this is the case, updating the JSONata library within Uptime Kuma could resolve the issue.
-
Data Type Mismatch: It's possible that Uptime Kuma is misinterpreting the data type of the
$count(orders)result. If the result is being treated as a boolean instead of a number, the comparison== 0might yield unexpected results. Ensuring correct data type handling within Uptime Kuma's code is crucial. -
Contextual Evaluation: The context in which the JSONata expression is evaluated within Uptime Kuma might differ from the context in the online JSONata Exerciser. This could be due to differences in the global scope or variable bindings. Careful examination of Uptime Kuma's code to understand the evaluation context is necessary.
-
Caching or Optimization Issues: Uptime Kuma might be employing caching mechanisms or optimizations that inadvertently affect the JSONata evaluation. For example, if the result of
$count(orders)is cached after the first evaluation, subsequent evaluations might return the cached value instead of recomputing it. Disabling or adjusting caching strategies could help identify this issue. -
Payload Parsing: Check for any discrepancies in how the JSON payload is parsed by Uptime Kuma versus the online JSONata tool. Inconsistent parsing could lead to the
ordersarray being misinterpreted or not being properly passed to the JSONata engine. Ensuring that Uptime Kuma's JSON parsing is robust and aligned with standards is essential.
Steps to Resolve
Here are some steps that can be taken to diagnose and resolve this problem:
- Debugging: Implement detailed logging within Uptime Kuma to trace the JSONata evaluation process. Log the raw JSON payload, the result of
$count(orders), and the final evaluation result. This will provide valuable insights into what's happening behind the scenes. - Version Check: Verify the exact version of the JSONata library used by Uptime Kuma. Compare it to the version used in the online JSONata Exerciser. If they differ, consider updating the library within Uptime Kuma.
- Unit Tests: Create unit tests specifically for JSONata evaluations within Uptime Kuma. These tests should cover various scenarios, including array counting and comparisons. This will help ensure that JSONata queries are evaluated correctly.
Importance of Accurate Monitoring
Accurate monitoring is paramount for maintaining the reliability and availability of web services. Monitoring tools like Uptime Kuma play a crucial role in detecting issues and alerting administrators before they impact users. When monitoring systems produce false negatives, critical problems can go unnoticed, leading to service disruptions and user dissatisfaction. Therefore, it's essential to address any discrepancies in monitoring behavior promptly and thoroughly.
Community and Contributions
The Uptime Kuma community is a valuable resource for addressing issues and improving the tool. If you encounter a bug or have a suggestion, consider reporting it on the Uptime Kuma GitHub repository. Contributing to open-source projects like Uptime Kuma helps ensure their continued development and reliability.
Conclusion
The JSONata query evaluation issue in Uptime Kuma highlights the importance of rigorous testing and verification in monitoring systems. While JSONata is a powerful tool, inconsistencies in its implementation or usage can lead to unexpected behavior. By understanding the potential causes of this issue and following the diagnostic steps outlined above, we can work towards ensuring the accuracy and reliability of Uptime Kuma's monitoring capabilities.
For more information on JSONata and its capabilities, consider visiting the official JSONata website. This external resource can provide additional insights and examples to help you effectively use JSONata in your projects.