ForHosting KIT · Developer Utilities

Balanced brackets validator

The balanced brackets validator checks parentheses, square brackets, and curly braces in code or plain text with one deterministic pass.

● BetaFree · in your browser
Use it from WebAPIEmailTelegramApp soon

It confirms whether every opening bracket has the right closing partner in the correct nesting order. If the input is invalid, the result identifies the first mismatch the scan can detect and gives its zero-based position, the character found, and the character expected. This makes the tool useful for quick manual checks, editor integrations, build scripts, teaching exercises, and lightweight validation pipelines without requiring a language-specific parser.

Understand what the validator checks

This validator focuses on the three bracket families most commonly used in source code and structured text: parentheses, square brackets, and curly braces. A valid input must close every opening character with a member of the same family, and closures must follow the reverse order in which openings appeared. For example, <code>({[]})</code> is balanced because each inner pair closes before its container, while <code>([)]</code> is not balanced because the parenthesis closes while the square bracket is still open. Characters other than these six bracket symbols do not affect the result. That makes it safe to paste a function, configuration fragment, mathematical expression, prose passage, or an entire multiline sample. The checker is deliberately lexical rather than language-aware: a bracket inside a quoted string or comment is still counted. This predictable rule works across programming languages, but it also means the tool is not a replacement for a JavaScript, Python, JSON, or compiler parser when quoted or commented brackets should be ignored.

Read mismatch positions and diagnostics

When a closing bracket cannot match the most recent opening bracket, validation stops immediately and reports that closing character as the first detected mismatch. The response includes <code>balanced: false</code>, a zero-based <code>position</code>, the <code>found</code> character, and what was <code>expected</code>. For an incorrect nesting such as <code>{[}</code>, the closing brace is reported at position two because a closing square bracket was required there. The response also provides the position of the opening bracket responsible for that expectation. If a closing bracket appears with no opening bracket available, the expected value explains that an opening bracket was required. An opening bracket left over at the end cannot be known to be unmatched until the scan finishes, so the result points to the most recently opened unclosed bracket and names the closing character it needs. JavaScript string indexing is used, which means positions are UTF-16 code-unit offsets; for ordinary ASCII source code, these are the familiar character offsets shown by most editors.

Use it in automation and review workflows

For an interactive check, paste code or text into the browser runner and inspect the structured response. The same deterministic solver is used by the API, so automation receives the same decision and diagnostic fields as a manual browser run. A pre-commit hook can reject generated snippets when <code>balanced</code> is false, a documentation pipeline can catch malformed examples before publication, and an educational application can use the expected character and exact position to give focused feedback. API requests cost $0.002; there is no variable per-character charge declared for this capability. Validation takes linear time because each character is visited once, while a stack stores only the opening brackets that have not yet been closed. The checker does not rewrite, repair, format, execute, or evaluate the submitted content. Treat its response as a precise bracket-structure signal and combine it with a language parser or linter when you also need grammar validation, awareness of string literals, comment handling, or suggestions for broader syntax errors.

Check generated code before saving

Reject a generated snippet when bracket nesting is incomplete or a closing bracket belongs to the wrong pair.

Give students focused feedback

Show the zero-based location and expected closing character instead of only saying that an expression is invalid.

Validate documentation examples

Scan code samples in a publishing pipeline and flag malformed bracket structure before readers see it.

Which characters are checked?

The validator checks parentheses (), square brackets [], and curly braces {}. All other characters are ignored.

Are positions zero-based?

Yes. Position zero is the first UTF-16 code unit in the supplied string, matching JavaScript string indexing.

Does it ignore brackets inside strings or comments?

No. This is a language-neutral lexical checker, so every bracket character is counted wherever it appears.

What happens when an opening bracket is never closed?

After scanning the complete input, the result identifies the most recently opened unmatched bracket and the closing character it requires.

What does an API request cost?

Each API request costs $0.002. The browser runner can perform the same deterministic check locally.

Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.

POSThttps://api.kit.forhosting.com/dev/balanced-brackets

Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.

curl -X POST https://api.kit.forhosting.com/dev/balanced-brackets \
  -H "Authorization: Bearer $KIT_KEY" \
  -H "Content-Type: application/json" \
  -d '{"text":"function demo(items) { return items[0]; }"}'
{
  "text": "function demo(items) { return items[0]; }"
}
{
  "task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
  "type": "dev.balanced_brackets",
  "status": "queued",
  "_links": {
    "result": "/tasks/tsk_…/result"
  }
}

The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.

Per request$0.002

Published price — no tokens, no invented credits. A failed task is never charged.

HTTPCodeMeaning
401unauthorizedMissing or invalid API key.
402insufficient_balanceYour balance doesn't cover the task price.
404unknown_typeThat task type doesn't exist.
429rate_limitedToo many requests. Use the webhook instead of polling.

Read the full KIT documentation →