Pass document: 1 to ApplyPatch to patch only the second document in a YAML stream. The index starts at zero.
dotnet add package OneYamlThe library targets .NET 8 and later; the example project uses .NET 10.
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 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:
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:
--- name: web replicas: 2 # public traffic --- name: worker replicas: 1 # background jobs
scale.json:
[ { "op": "test", "path": "/name", "value": "worker" }, { "op": "replace", "path": "/replicas", "value": 3 } ]
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:
--- 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:
var patch = new YamlPatch().Replace("/replicas", 3); doc.ApplyPatch(patch, document: 1);
Related recipes
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 lists both APIs and the rules for YAML values.