---
title: "Apply JSON Patch to one YAML document in C#"
url: https://recursiveai.org/oneyaml/csharp/cookbook/apply-yaml-patch-to-stream-document/
description: "Patch the second document of a multi-document YAML file in C# by index, keeping the first document and surrounding comments unchanged."
version: "1.2.1"
---

# Apply JSON Patch to one YAML document in C#

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

[NuGet](https://www.nuget.org/packages/OneYaml) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-csharp) · [Docs](https://recursiveai.org/oneyaml/csharp/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/csharp/cookbook/apply-yaml-patch-to-stream-document/apply-yaml-patch-to-stream-document.zip)

Pass `document: 1` to `ApplyPatch` to patch only the second document in a YAML stream. The index starts at zero.

```shell
dotnet add package OneYaml
```

The library targets .NET 8 and later; the example project uses [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0).

```csharp
using OneYaml;

using var doc = YamlDocument.Parse(
    "replicas: 2\n---\nreplicas: 1\n");
var patch = """
    [{ "op": "replace", "value": 3,
       "path": "/replicas" }]
    """;
doc.ApplyPatch(patch, document: 1);
Console.Write(doc);
```

[The example](https://recursiveai.org/oneyaml/csharp/cookbook/apply-yaml-patch-to-stream-document/apply-yaml-patch-to-stream-document.zip) is the full program below: unzip it and `dotnet run`. To run only the code above, paste it into Program.cs of a new `dotnet new console` project and `dotnet run`.

Prints:

```text
replicas: 2
---
replicas: 3
```

## The full program

The download includes `services.yaml` and `scale.json`. The patch first checks the name of the second service, then changes its replica count.

`services.yaml`:

```yaml
---
name: web
replicas: 2 # public traffic
---
name: worker
replicas: 1 # background jobs
```

`scale.json`:

```json
[
  { "op": "test", "path": "/name", "value": "worker" },
  { "op": "replace", "path": "/replicas", "value": 3 }
]
```

```csharp
using OneYaml;

using var input = File.OpenRead("services.yaml");
using var doc = YamlDocument.Parse(input);
var patch = File.ReadAllText("scale.json");
doc.ApplyPatch(patch, document: 1);
File.WriteAllText("services-patched.yaml", doc.ToString());
Console.Write(doc);
```

The program writes `services-patched.yaml` and leaves `services.yaml` on disk unchanged. It prints:

```text
---
name: web
replicas: 2 # public traffic
---
name: worker
replicas: 3 # background jobs
```

If the index doesn’t exist, `ApplyPatch` throws `YamlException` with category `PatchPath` and the program doesn’t write an output file.

## Variations

If you’re building the operations in C# instead of reading JSON text, replace the `patch` declaration and `ApplyPatch` call in the snippet at the top:

```csharp
var patch = new YamlPatch().Replace("/replicas", 3);
doc.ApplyPatch(patch, document: 1);
```

## Related recipes

- [Apply a JSON Patch to a YAML file](https://recursiveai.org/oneyaml/csharp/cookbook/patch-yaml/)
- [Rewrite YAML documents by index](https://recursiveai.org/oneyaml/csharp/cookbook/rewrite-yaml-documents-in-stream-by-index/)

## The short answer

Parse the whole YAML stream and call `doc.ApplyPatch(patch, document: 1)` for its second document. Use `/replicas` to point to the value inside that document; the JSON Pointer doesn’t include the document index. Write the edited stream back to a file only after the patch succeeds. The sample writes a separate output file so you can compare it with the original.

## How do I patch a multi-document YAML file without changing the other documents?

Use the `document` argument to select the document, then write each patch path relative to its root. Omitting the argument targets the first document, as does `document: 0`. A patch never runs across all documents just because they have the same key. In the example, `/replicas` points to the worker’s count because the second document is selected. The web service above it keeps its original count and comment, and the worker’s comment stays in place when its number changes.

If the order of the documents can change, put a `test` operation first to check a field that identifies the document you expect. The sample checks `/name` for `worker`. It doesn’t search for the worker: it refuses to make the change if the document at index 1 isn’t the worker. OneYaml applies the operations in order, and a failed operation leaves the text as it was before that patch. A separate call for another document is a separate patch, not part of the same atomic operation.

## What is the difference between JSON Patch and JSON Merge Patch for YAML?

JSON Patch uses an ordered array of operations and paths. Use it when you want to check a value before editing, or change one position in a sequence. A merge patch instead uses a value shaped like the desired override: an object merges keys, a `null` member removes a key, and an array replaces the whole sequence. OneYaml also lets a merge patch select one document by index. The [patch guide](https://recursiveai.org/oneyaml/csharp/docs/patches/) lists both APIs and the rules for YAML values.
