---
title: "Escape a slash in a YAML key’s JSON Pointer in Go"
url: https://recursiveai.org/oneyaml/go/cookbook/patch-yaml-key-with-slash-in-name/
description: "Patch a YAML key containing a slash in Go. Use JSON Pointer’s ~1 escape for a literal slash and ~0 for a tilde inside a key."
version: "1.0.0"
---

# Escape a slash in a YAML key’s JSON Pointer in Go

**oneyaml-go** is a free, open-source YAML library for Go from Recursive AI. MIT or Apache-2.0, no dependencies.

[pkg.go.dev](https://pkg.go.dev/oneyaml.dev/go) · [GitHub](https://github.com/recursive-ai-oss/oneyaml-go) · [Docs](https://recursiveai.org/oneyaml/go/docs/) · [Download example (.zip)](https://recursiveai.org/oneyaml/go/cookbook/patch-yaml-key-with-slash-in-name/patch-yaml-key-with-slash-in-name.zip)

In a JSON Pointer, `/` separates keys and `~1` means a slash inside a key. Pass the raw keys to `Pointer` so it escapes each one for you.

```shell
go get oneyaml.dev/go@v1.0.0
```

Go 1.24+.

```go
package main

import (
	"fmt"

	yaml "oneyaml.dev/go"
)

func main() {
	input := "labels:\n  team/name: ops\n"
	doc, err := yaml.Parse([]byte(input))
	if err != nil {
		panic(err)
	}
	path := yaml.Pointer("labels", "team/name")
	op := yaml.PatchReplace(path, "platform")
	if err := doc.Apply(op); err != nil {
		panic(err)
	}
	fmt.Println(path)
	fmt.Print(doc)
}
```

Paste the code above into main.go, run `go mod init example` and `go get oneyaml.dev/go@v1.0.0`, then `go run .`; or unzip [the example](https://recursiveai.org/oneyaml/go/cookbook/patch-yaml-key-with-slash-in-name/patch-yaml-key-with-slash-in-name.zip) and `go run .`.

`go run .` prints:

```text
/labels/team~1name
labels:
  team/name: platform
```

The program prints the pointer and the edited YAML. If you omit the escape, the patch looks for a nested `name` under `team` instead; it fails in this document.

## Variations

**Write a fixed path by hand.** Replace the `path` line with the escaped string:

```go
path := "/labels/team~1name"
```

It prints:

```text
/labels/team~1name
labels:
  team/name: platform
```

## Related recipes

- [Patch YAML in Go](https://recursiveai.org/oneyaml/go/cookbook/patch-yaml/)
- [Locate a YAML value in the source](https://recursiveai.org/oneyaml/go/cookbook/locate-yaml-value-in-source/)

## The short answer

Use `~1` for a slash inside a single [JSON Pointer](https://www.rfc-editor.org/rfc/rfc6901#section-3) token and `~0` for a tilde. In Go, give `Pointer` one raw key per argument. The helper inserts the separating slashes and escapes the literal characters inside each argument. Use the returned string as the patch path.

## How do I escape special characters in a JSON Pointer?

A pointer begins with `/` for the first token and then uses `/` to separate tokens. Here `/labels/team~1name` means two keys: `labels` and `team/name`. `/labels/team/name` means three keys, so it can address a different value in a document that has that nesting. When the input key comes from a variable, pass it to `Pointer` instead of joining raw strings with `/`.

The other character that needs a pointer escape is `~`. A key literally named `team~1name` becomes the token `team~01name`, which isn’t the same as `team~1name`. If you build a token yourself, encode tildes before slashes so you don’t escape the tildes you’ve just added. If you call `Pointer`, pass unescaped keys: giving it `team~1name` would target the key with the literal tilde, not the key with the slash.

## Do I need to quote a YAML key with a slash?

Not for the slash itself: `team/name: ops` is a plain YAML key. If the key were quoted as `"team/name"`, the quotes would mark its YAML style but wouldn’t be part of the key’s text. Both spellings use `/labels/team~1name` as the path. YAML quoting, JSON string escaping, and JSON Pointer escaping are separate: only the last one determines which slash separates tokens. If the pointer is embedded in JSON patch text, it’s still a JSON string and gets the usual surrounding JSON quotes.
