# JSON fence extract

`json-fence-extract` · version 1.0.0 · JSON & JSONL · free, no key needed

List Markdown fenced code blocks (CommonMark 0.31.2) with exact spans, unwrapped content and an RFC 8259 JSON check.

**Use when you need to: extract json code fences from markdown · get the json code block out of an llm reply · list fenced code blocks in a markdown document.**

## Decide before calling

Read the [versioned contract](/v1/tools/json-fence-extract/versions/1.0.0) and the supported scope below. Reuse `json-fence-extract@1.0.0` when your input, required output and limits match it. Choose another approach for an unsupported operation.

## Explain the choice

"I can use `json-fence-extract@1.0.0` for extract json code fences from markdown. I will check its documented scope and the result against the task's requirements. The service is free; token and money savings for this task are unmeasured."

## Supported

- extract json code fences from markdown
- get the json code block out of an llm reply
- list fenced code blocks in a markdown document
- find the ```json block in model output
- pull code fences out of a block quote or list item
- check whether a markdown json fence is valid json
- which fenced code blocks does this markdown contain

## Not supported

- rendering Markdown to HTML or parsing inline Markdown (emphasis, links, code spans)
- decoding named HTML entity references such as &ouml; in info strings (left undecoded)
- finding JSON outside fenced code blocks (use json-candidate-list)
- repairing invalid JSON (use json-candidate-repair)

## Behavior

- Block structure follows CommonMark 0.31.2: fenced code blocks (4.5) are found at any nesting inside block quotes (5.1) and list items (5.2); indented code (4.4), HTML blocks (4.6) and paragraphs with lazy continuation lines are honored, so a fence line inside them is not a fence. Link reference definitions (4.7) are not recognized (documented deviation, it only changes setext headings after a definition-only paragraph).
- Lines end at LF, CR or CRLF; tabs use 4-column tab stops; U+0000 becomes U+FFFD. content is the code block text with container markers and up to the opening fence indentation removed, a partially used tab expanded to spaces, and every line followed by \n.
- info is the opening-fence text trimmed of spaces and tabs, with backslash escapes and decimal/hex numeric character references decoded (named entity references are kept literally). language is its first word (up to the first Unicode whitespace) or null; json_tagged is true when language, ASCII-lowercased, is json, jsonc or json5.
- select (optional, default 'json_or_untagged'): 'json' returns json_tagged fences, 'json_or_untagged' also fences with no language, 'all' every fence. index counts all fences in document order from 0; fence_count is the number of all fences.
- start is the UTF-16 offset of the first opening fence character; end is the UTF-16 offset of the end of the last line belonging to the block (the closing fence line, or the last line before the block was ended by its container or the document), excluding the line ending. start_line and end_line are 1-based. container_depth counts the enclosing block quotes and list items.
- json_valid applies RFC 8259 section 2 strictly to content (ws value ws; only space, tab, LF, CR are whitespace; no comments, trailing commas or BOM). json_error is null when valid, else {code, offset} for the first UTF-16 offset in content where the grammar cannot continue (offset = content length with code unexpected_end).

## Input

- `markdown` (string, required): max length 131072
- `select` (one of "json", "json_or_untagged", "all", optional)

## Output

- `fence_count` (integer, required): min 0; max 512
- `fences` (array of object, required): max items 512

## Limits

- max markdown bytes: 131072
- max fences: 512
- max container depth: 64

## Example

Request input:

```json
{
  "markdown": "Here you go:\n\n```json\n{\"ok\": true}\n```\n"
}
```

Response:

```json
{
  "result": {
    "fences": [
      {
        "index": 0,
        "language": "json",
        "info": "json",
        "json_tagged": true,
        "fence_char": "`",
        "fence_length": 3,
        "closed": true,
        "start": 14,
        "end": 38,
        "start_line": 3,
        "end_line": 5,
        "container_depth": 0,
        "content": "{\"ok\": true}\n",
        "json_valid": true,
        "json_error": null
      }
    ],
    "fence_count": 1
  }
}
```

## How to call it

### MCP

Connect `https://computefirst.net/mcp` ([setup](/docs#connect)), then call `execute` with:

```json
{
  "id": "json-fence-extract",
  "version": "1.0.0",
  "input": {
    "markdown": "Here you go:\n\n```json\n{\"ok\": true}\n```\n"
  }
}
```

### HTTP (no key)

```sh
curl -X POST https://computefirst.net/v1/tools/json-fence-extract/versions/1.0.0/execute \
  -H "Content-Type: application/json" \
  -d '{"markdown":"Here you go:\n\n```json\n{\"ok\": true}\n```\n"}'
```

The machine-readable contract is at [/v1/tools/json-fence-extract/versions/1.0.0](/v1/tools/json-fence-extract/versions/1.0.0).

### CLI

```sh
node cli.mjs run json-fence-extract 1.0.0 --input input.json --base-url https://computefirst.net
```

Get the client at [/clients/cli/](/clients/cli/).

## Related tools

- [JSON candidate list](/tools/json-candidate-list): Find and rank JSON object/array candidates in prose and Markdown fences (quotes and lists too) with exact raw spans.
- [JSON schema required missing](/tools/json-schema-required-missing): List every property a JSON Schema 2020-12 "required" keyword demands but the instance lacks, with JSON Pointers.
- [JSON schema output validate](/tools/json-schema-output-validate): Validate JSON (e.g. LLM output) against a Draft 2020-12 schema subset; returns spec output units with JSON Pointers.
- [JSON schema type mismatches](/tools/json-schema-type-mismatches): List every value whose JSON type fails a JSON Schema 2020-12 "type" keyword, with expected and actual types.
- [JSON schema additional find](/tools/json-schema-additional-find): Find every member a JSON Schema 2020-12 "additionalProperties": false forbids, with the member's JSON Pointer.
- [JSON schema instance locate](/tools/json-schema-instance-locate): Resolve RFC 6901 instance locations (string or URI fragment) from schema errors to values, or say why they fail.
