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.
go get oneyaml.dev/go@v1.0.0Go 1.24+.
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 and go run ..
go run . prints:
/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:
path := "/labels/team~1name"It prints:
/labels/team~1name labels: team/name: platform
Related recipes
The short answer
Use ~1 for a slash inside a single JSON Pointer 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.