Recursive AI

Export Chrome-shaped accessibility JSON in C#

onehtml is a free, open-source HTML library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

Export static HTML roles and names as Chrome-shaped JSON, without running Chromium or page scripts. “Chrome-shaped” means the same field layout as the DevTools Protocol AXNode, not data taken from a live browser.

Terminal
dotnet add package RecursiveAI.OneHtml.Accessibility

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Css;
using RecursiveAI.OneHtml.Accessibility;
 
var doc = Document.Parse(
    "<button>Save</button>");
var tree = AccessibilityTree.Build(
    doc, DocumentStyles.Create(doc));
File.WriteAllText("accessibility.json",
    tree.ToChromeJson());
Console.WriteLine("Wrote accessibility.json");
Text
Wrote accessibility.json

Unzip the example, then dotnet run.

The full program

The full program writes accessibility.json, reads that JSON back and prints its schema and non-ignored nodes. The HTML input is the inline <button>Save</button>.

C#
using RecursiveAI.OneHtml;
using RecursiveAI.OneHtml.Css;
using RecursiveAI.OneHtml.Accessibility;
using System.Text.Json;
 
var doc = Document.Parse("<button>Save</button>");
var tree = AccessibilityTree.Build(
    doc, DocumentStyles.Create(doc));
string json = tree.ToChromeJson();
File.WriteAllText("accessibility.json", json);
using var parsed = JsonDocument.Parse(json);
var root = parsed.RootElement;
Console.WriteLine($"Schema: {root.GetProperty("schema").GetString()}");
foreach (var node in root.GetProperty("nodes").EnumerateArray())
    if (!node.GetProperty("ignored").GetBoolean())
        Console.WriteLine($"{node.GetProperty("role").GetProperty("value").GetString()}: " +
            node.GetProperty("name").GetProperty("value").GetString());
Console.WriteLine("Wrote accessibility.json");

dotnet run prints:

Text
Schema: onehtml-ax/1
RootWebArea: 
button: Save
StaticText: Save
Wrote accessibility.json

This excerpt of accessibility.json shows the schema wrapper and the button’s ID, role and name. Other nodes and fields, including the name’s sources, are omitted.

JSON
{
  "schema": "onehtml-ax/1",
  "nodes": [
    {
      "nodeId": "3",
      "role": {
        "type": "role",
        "value": "button"
      },
      "name": {
        "type": "computedString",
        "value": "Save"
      }
    }
  ]
}

The file write throws an I/O exception if the destination cannot be written.

Variations

Write indented JSON

Append this to the full program to create a more readable copy of the same export.

C#
string pretty = JsonSerializer.Serialize(
    parsed.RootElement,
    new JsonSerializerOptions { WriteIndented = true });
File.WriteAllText("accessibility-pretty.json", pretty);
Console.WriteLine("Wrote accessibility-pretty.json");
Text
Wrote accessibility-pretty.json

The short answer

Call AccessibilityTree.ToChromeJson() and write the returned string to a file. onehtml builds the accessibility tree from parsed HTML and computed styles, then exports nodes in the documented Chrome-shaped interchange format. No browser process is required for this static path.

The result has a wrapper object with schema set to onehtml-ax/1 and a nodes array. Read the array when your consumer needs accessibility nodes; do not treat the whole file as an unwrapped protocol response. The full program reads the export back with System.Text.Json so you can see both the schema and the exposed roles and names.

How do you export an accessibility tree as JSON in C#?

Parse your HTML with Document.Parse, create DocumentStyles for that document, then pass both to AccessibilityTree.Build. Call ToChromeJson on the resulting tree. The snippet at the top contains the entire input and writes the file in the working directory.

The export is deterministic. It retains tree order and includes ignored nodes rather than pruning them. Each entry has a string nodeId; parent and child fields connect those entries. The full program’s console loop filters ignored nodes only for display. The saved JSON still contains them.

The indented variation changes whitespace, not the exported information. Keep the original compact string when file size matters, or write the indented form when you want to inspect it in a text editor. Both can be read with the same JSON parser.

Is this the same as Accessibility.getFullAXTree?

It is Chrome-shaped, not a live Chrome response. The export documentation maps the array to Chrome DevTools Protocol AXNode fields. Roles use typed objects, names use computed-string values with a source, and states appear as typed properties. A consumer should read those structures rather than assuming that every value is a plain string.

There are important identity differences. backendDOMNodeId is an index in the onehtml document, not a Chrome backend node ID. It is absent for the document node. Do not pass these values to a connected browser as if they identified its DOM nodes.

The documented export omits InlineTextBox nodes, frameId, chromeRole, focused, the hiddenRoot and root properties, and name sources that did not win. A reader requiring those fields needs a different source of information; changing JSON formatting cannot supply them.

Why does the Chrome accessibility tree look different?

Chrome DevTools describes its tree as a live view. It updates as the renderer changes and filters ignored or uninteresting nodes for display. The static sample here neither executes JavaScript nor captures runtime changes. Comparing its full JSON array directly with a filtered visual tree can therefore be misleading.

Layout is another distinction. Without rendered facts, onehtml builds its tree without layout. With facts captured from the same document and style context, it can add documented layout-derived nodes, including list markers and a select’s popup, and apply layout-dependent pruning. Those generated nodes do not have a backendDOMNodeId.

The export does not include bounding boxes, off-screen state or a scrollable property. The documentation notes that Chrome’s getFullAXTree has none of those either. Use the export for inspecting the documented semantic fields, not for locating controls on screen or reconstructing a rendered page.

All onehtml recipes