knowledge/technology/files/JSON Pointer.md

25 lines
1.2 KiB
Markdown
Raw Normal View History

2023-12-04 10:02:23 +00:00
---
obj: concept
rfc: https://datatracker.ietf.org/doc/html/rfc6901
---
# JSON Pointer
JSON Pointer defines a string format for identifying a specific value within a [JSON](JSON.md) document.
A JSON Pointer is a string of tokens separated by `/` characters, these tokens either specify keys in objects or indexes into arrays. For example, given the [JSON](JSON.md)
```json
{
"biscuits": [
{ "name": "Digestive" }`
{ "name": "Choco Leibniz" }
]
}
```
2024-01-17 08:44:04 +00:00
`/biscuits` would point to the array of biscuits and `/biscuits/1/name` would point to `"Choco Leibniz"`.
2023-12-04 10:02:23 +00:00
2024-01-17 08:44:04 +00:00
To point to the root of the document use an empty string for the pointer. The pointer `/` doesnt point to the root, it points to a key of `""` on the root (which is totally valid in [JSON](../files/JSON.md)).
2023-12-04 10:02:23 +00:00
2024-01-17 08:44:04 +00:00
If you need to refer to a key with `~` or `/` in its name, you must escape the characters with `~0` and `~1` respectively. For example, to get `"baz"` from `{ "foo/bar~": "baz" }` youd use the pointer `/foo~1bar~0`.
2023-12-04 10:02:23 +00:00
2024-01-17 08:44:04 +00:00
Finally, if you need to refer to the end of an array you can use `-` instead of an index. For example, to refer to the end of the array of biscuits above you would use `/biscuits/-`. This is useful when you need to insert a value at the end of an array.