Can I validate a JSON response field by field in a health check (Content Check Mode: JSONata Expression)?
Short answer
Yes. In the HTTP or HTTPS profile, JSONata Expression is selected in the Content Check Mode field and the expression is written in the JSONata Query field (e.g. body.status = "UP"). The expression uses the status (status code), headers (lowercase headers), body (body parsed as JSON or XML) and bodyRaw (raw body) values; if the result is true (truthy), the content check is successful. The Expected Status Codes list is also applied separately. In WEB-CLI, it is written with --contentCheckMode jsonata and --jsonataQuery.
Problem: The Health Response Carries the Status of More Than One Component
The health addresses of modern applications often return JSON: there is a separate status field for each of the components such as the database, the cache and the queue. A fixed text search cannot reliably check this structure (the field order and the spaces can change); a decision based on the values of specific fields is needed. Otherwise, a server with a component that is not working continues to receive traffic.
Solution: 2 Methods
The two methods make the same setting from different interfaces.
| Your situation | Suitable method |
|---|---|
| I configure the profile from the interface; I want to try the expression on a sample response | Method 1: JSONata Expression |
| I create or update the profile from WEB-CLI | Method 2: WEB-CLI |
Method 1: JSONata Expression Recommended
- How it works The expression is evaluated on every response; if the result is true (true, non-empty text, a non-zero number, an object or an array), the content check is successful. If the result is false, the check fails with "JSONata check returned false", and if the expression is broken, with "JSONata evaluation error".
- Where it is openedLoad Balancer → Profiles → Health Checks → HTTP/HTTPS profile → Content Check Mode → JSONata Expression → JSONata Query
- Requirement The health address returning JSON (or XML); a Backend Service to which the profile will be assigned
- vService type All
Step-by-Step Configuration
The Configuration Menu is opened with the TR7 logo at the bottom left; the Load Balancer → Profiles → Health Checks path is followed.
For a new profile, Add is clicked; for an existing HTTP/HTTPS profile, the profile is selected and Edit is clicked.
In a new profile, Name is entered, HTTP or HTTPS is selected in the Check Type field, and the health address is entered in the Path + Query field (e.g. /health). In an existing profile, this step is skipped.
JSONata Expression is selected in the Content Check Mode field; the JSONata Query field appears in place of the Expected Content Query field.
The expression is written and tried on a sample response in the editor of the field. Examples:
- All components UP:
status = 200 and $exists(body.checks) and $count(body.checks[status != "UP"]) = 0 - Status code and field together:
status = 200 and body.status = "UP" - Header check:
$contains(headers.`content-type`, "json")
The Add (Edit when editing) button is clicked; the change is applied to all the Backend Services that use the profile. If an existing profile was edited, the procedure ends here.
For a new profile, on the Load Balancer → Backend Services screen, the Backend Service is right-clicked and Edit is selected.
The profile is selected in the Health Check field.
The Edit button is clicked.
Verification
The indicator in the Health column of the Backend Services list is checked; if the expression is not met, a JSONata error appears in the Health Check card of the preview (Health status indicator).
OK (green)Method 2: WEB-CLI
- How it works The mode is written with
--contentCheckMode jsonataand the expression with--jsonataQuery;--jsonataQueryis used only in an HTTP/HTTPS profile and while the mode is jsonata. - Where it is opened WEB-CLI →
lb hc add/lb hc update(lb hc · The HTTP and HTTPS Fields) - Requirement WEB-CLI access; the expression must not contain the
|character (WEB-CLI treats the|sign as a command separator; such expressions are entered from the interface) - vService type All
Step-by-Step Configuration
New profile (checkType and name are required and are written in this order):
lb hc add checkType http name json-saglik --uri /health --contentCheckMode jsonata --jsonataQuery 'body.status = "UP"'In an existing HTTP/HTTPS profile (the ID is taken from the lb hc list output):
lb hc update id <profile-id> --contentCheckMode jsonata --jsonataQuery 'body.status = "UP"'lb backend update id <backend-id> --hc <profile-id>Verification
The profile record is displayed.
lb hc show <profile-id>contentCheckMode jsonata, jsonataQuery body.status = "UP"Common Mistakes
- Assuming that the body is always parsed as JSON. The body is parsed as JSON if the Content-Type contains
jsonor if there is no Content-Type; in a JSON returned astext/plain,body.fieldis undefined, and in this case$eval(bodyRaw).fieldis used. - Not considering the absence case. The
$count(body.checks[status != "UP"]) = 0expression also returns true if thechecksfield does not exist at all (e.g. when an error page is returned); with$exists(body.checks) and …, the existence of the field is also checked. - Writing header names in uppercase. The names in
headersare lowercase. - Checking the status code only in the expression. The Expected Status Codes list is also applied separately.
- Assuming that only a
trueresult counts as successful. Non-empty text or a non-zero number also counts as successful; the expression must contain an explicit comparison.
How it is done in the documentation
