---
title: "Export a replay comparison to JSON"
url: https://recursiveai.org/onescope/cookbook/export-replay-comparison-as-json/
description: "Compare two local simulation replays with OneScope in headless mode and save just the comparison result as a JSON file."
version: "1.0.0"
---

# Export a replay comparison to JSON

**OneScope** is a free, open-source viewer for simulation recordings, with a desktop window or command-line control, from Recursive AI. MIT or Apache-2.0.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneScope.Tool) · [GitHub](https://github.com/recursive-ai-oss/onescope-csharp) · [Docs](https://recursiveai.org/onescope/docs/) · [Download example (.zip)](https://recursiveai.org/onescope/cookbook/export-replay-comparison-as-json/export-replay-comparison-as-json.zip)

Run `compare` as the last `--run` operation and keep only its JSON output. Headless mode still needs a graphics driver.

```shell
dotnet tool install -g RecursiveAI.OneScope.Tool
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

A module is the plugin that reads your `.sim` format. In the unzipped example folder, build the sample module before opening the recordings:

```bash
set -o pipefail
dotnet build Module -o obj/module
module=obj/module/CounterModule.dll
onescope open before.sim --headless \
  --module "$module" \
  --run 'open {"path":"after.sim"}' \
  --run 'compare {"source":0,"other":1,
    "kind":"shared","step":1}' \
  | jq -es 'last | select(.kind=="shared")' \
  > comparison.json
```

Unzip [the example](https://recursiveai.org/onescope/cookbook/export-replay-comparison-as-json/export-replay-comparison-as-json.zip), then `dotnet run`.

Writes `comparison.json`. The shell snippet uses Bash and `jq`; the zip’s `dotnet run` uses neither. The zip includes `before.sim`, `after.sim` and the project for the counter module that reads them. For recordings accepted by a bundled module, leave out `--module "$module"`.

On Linux, headless rendering can use a Vulkan device or a software renderer such as lavapipe.

## The full program

**`before.sim`**

    {"entities":1,"bias":0}

**`after.sim`**

    {"entities":1,"bias":1}

`before.sim` starts the counter at zero; `after.sim` starts it at one. The sample module increments both at each step. The example builds the sample module against `RecursiveAI.OneScope.Sdk` 1.0.0 and installs a private copy of OneScope. To create a module project of your own, install its SDK dependency:

```shell
dotnet add package RecursiveAI.OneScope.Sdk
```

```csharp
using System.Diagnostics;
using System.Text.Json;

const string module = "Module/bin/Debug/net10.0/CounterModule.dll";
string tools = Path.GetFullPath("obj/tools");
string tool = Path.Combine(tools,
    OperatingSystem.IsWindows() ? "onescope.exe" : "onescope");
if (!File.Exists(tool))
    Run("dotnet", "tool", "install", "RecursiveAI.OneScope.Tool",
        "--tool-path", tools, "--version", "1.0.0");
Run("dotnet", "build", "Module/CounterModule.csproj", "-c", "Debug");

string output = Run(tool, "open", "before.sim", "--module", module,
    "--headless", "--run", "open {\"path\":\"after.sim\"}",
    "--run", "compare {\"source\":0,\"other\":1,\"kind\":\"shared\",\"step\":1}");
string comparison = output.Split('\n')[^1];
using var json = JsonDocument.Parse(comparison);
if (json.RootElement.GetProperty("kind").GetString() != "shared")
    throw new InvalidOperationException("Expected a shared comparison.");
File.WriteAllText("comparison.json", json.RootElement.GetRawText());
Console.WriteLine("Wrote comparison.json");

static string Run(string command, params string[] args)
{
    var start = new ProcessStartInfo(command)
    {
        RedirectStandardOutput = true
    };
    foreach (string arg in args) start.ArgumentList.Add(arg);
    using var process = Process.Start(start)!;
    string output = process.StandardOutput.ReadToEnd();
    process.WaitForExit();
    if (process.ExitCode != 0)
        throw new InvalidOperationException($"{command} failed: {output}");
    return output.TrimEnd();
}
```

```text
Wrote comparison.json
```

The `difference` object in `comparison.json` at step 1 is:

```json
{
  "entity": 2,
  "field": "Value",
  "source": "1",
  "other": "2"
}
```

If an open or compare operation fails, OneScope exits nonzero; the Bash command may leave an empty output file, so check its exit status before using the file. The C# launcher writes the file only after a successful comparison.

## Variations

To report the first divergent step instead of a fixed step, use this as the last `--run` argument in the command above:

```text
compare {"source":0,"other":1,
  "kind":"shared","goto":"first"}
```

For these inputs, `step` and `agreement.firstDivergent` are both `0`: the starting values already differ.

## Related recipes

- [Compare shared entities](https://recursiveai.org/onescope/cookbook/compare-shared-entities/)
- [Compare library versions](https://recursiveai.org/onescope/cookbook/compare-library-versions/)
- [Load a custom simulation replay module DLL](https://recursiveai.org/onescope/cookbook/load-custom-replay-module-from-dll/)

## The short answer

Open the first recording with `onescope open --headless`, open the second through `--run`, then make `compare` the last operation. OneScope prints a JSON result for the initial open and each subsequent operation. Keep only the last result so your export is one JSON object, not a stream of separate responses.

## How do I save only the comparison response as JSON?

Redirecting the command straight to a file saves the two open responses as well as the comparison. In the Bash example, `jq -s` collects all the responses in an array and `last` selects the comparison. The filter checks for the expected comparison kind; `-e` makes a missing result an error. `pipefail` also catches a failure in OneScope even though `jq` is the last command in the pipe. Keep `compare` last when adding operations, or change how you select the response.

## How do I find what changed between two simulation replays?

For two recordings with matching entity ids, `kind:"shared"` compares the entities that both recordings contain. Entities that exist on only one side are counted in `onlySource` and `onlyOther`; they don’t appear as differing fields. An `equal` result with this comparison kind doesn’t mean the whole worlds are identical. For example, a partial recording can agree with a full recording on every shared entity.

`difference` is the first differing field at the selected step. `rows` gives more inspector differences for that step, up to `limit`. Use `rowCount` and `truncated` to tell how many were omitted. To find when the runs first disagreed, use `agreement.firstDivergent` instead. Check `agreement.scannedThrough` and `agreement.complete` before treating it as a summary of the full interval: [the shell docs](https://recursiveai.org/onescope/docs/shell/#compare) cap the scan for a shared comparison at 10,000 steps.
