NBT path
An NBT path is a descriptive string used to specify one or more particular elements from an NBT data tree.
Usage
The format of an NBT path is node.node.….node, i.e. one or more nodes represented by . (dot/period) characters. Dots before some certain nodes (e.g. []. See table below) can be omitted. Each node declares what kinds of child tags should be selected from what kinds of current tags. When a node is applied on a tag, one or more child tags in it is selected.
Tags without tag names are aggregated into a collection, which is called a tag collection. The initial tag collection has only one element (the root tag). Each node in an NBT path is applied on the elements in the tag collection to generate a new tag collection. As nodes in the NBT path are applied one by one, the tag collection is constantly changing. After the last node is applied, the tags in the new tag collection are the tags selected by the NBT path.
If there are multiple elements in current tag collection, a node is applied on each element in the tag collection separately, and all the selected child tags are placed into the same new tag collection. For example: for {tag1:1b, tag2:[{foo:0},{foo:[]},{foo:{}}]}, path tag2 selects only one tag: a list tag [{foo:0},{foo:[]},{foo:{}}]; Path tag2.[] selects three tags: three compound tags {foo:0}, {foo:[]}, and {foo:{}}; If adding another node after this path, the new node is applied on each of these three tags, e.g. path tag2.[].foo selects three tags: an integer tag 0, a list tag [], and a compound tag {}.
Some commands like /data get ... require the size of the tag collection obtained by the NBT path to be 1, that is, only one tag is selected.
And some commands like /data modify ... allow the size of the tag collection obtained by the NBT path to be greater than 1, that is, modifying more than one tags at once is allowed.
Nodes
These are all six types of nodes available. Needed nodes can be used arbitrarily, except the root compound tag node is only applicable as the first element in an NBT path.
| Description | Format | When applied on a tag | Size of the new tag collection | Example | Example Description |
|---|---|---|---|---|---|
| Root compound tag | {tags}, where tags is used to match the content of the root tag (see NBT format#Testing NBT tags) and can be empty.
|
Selects the root tag, only if it matches the specified content tags (if given). | Only one element (the root compound tag) or empty if matching fails. | {Invisible:1b}
|
Selects the root tag if it has a child tag [Byte] Invisible with value 1.
|
{}
|
Selects the root tag. | ||||
| Named tag in current compound tag | name, where name can quoted by ' or ".
|
Selects the child tag named name from the current tag, if the current tag is a compound tag. | Less than or equal to the current tag collection. May be empty. | VillagerData
|
Selects the child tag named VillagerData. |
"A cool name[]"
|
Selects the child tag named A cool name[]. | ||||
| Named compound tag in current compound tag | name{tags}, where name can quoted by ' or "; tags is used to match the content of the root tag (see NBT format#Testing NBT tags) and can be empty.
|
Selects the child tag named name from the current tag, if the current tag is a compound tag, and if the named child tag is a compound tag and matching the specified content tags (if given). | All elements are compound type. Less than or equal to the current tag collection. May be empty. | VillagerData{foo:text}
|
Selects the child tag [NBT Compound / JSON Object] VillagerData only if it has a child tag [String] foo with value text.
|
VillagerData{}
|
Selects the child tag [NBT Compound / JSON Object] VillagerData if it is a compound tag. | ||||
| All elements in current list or array tag | []Dot before this node can be omitted. |
Selects all elements of the current tag, if the current tag is a list or array tag. | Less than, equal to, or greater than the current tag collection. May be empty. | ActiveEffects[]
|
Select all the elements in the child tag "ActiveEffects". |
| Element in current list or array tag | [index], where index is an integer.Dot before this node can be omitted. |
Selects the element at index (or listLength + index when index is negative) of the current tag, if the current tag is a list or array tag.
|
Less than or equal to the current tag collection. May be empty. | Pos.[0]Or Pos[0]
|
Selects the first element in the child tag "Pos" list (or array). |
Inventory.[-1]Or Inventory[-1]
|
Selects the last element in the child tag "Inventory" list (or array). | ||||
| Compound elements in current list | [{tags}], where tags is used to match the content of the root tag (see NBT format#Testing NBT tags) and can be empty.Dot before this node can be omitted. |
Selects compound elements that matching the specified content tags (if given) from the current tags, if the current tag is a list tag. | Less than, equal to, or greater than the current tag collection. May be empty. | Inventory.[{Count:25b}]Or Inventory[{Count:25b}]
|
Selects [NBT Compound / JSON Object]compound elements that have a child tag [Byte] Count with value 25 from the child tag [NBT List / JSON Array] Inventory.
|
Foo.[{}]Or Foo[{}]
|
Selects [NBT Compound / JSON Object]compound elements in the child tag [NBT List / JSON Array] Foo. | ||||
foo.[].[{}]Or foo.[][{}]Or foo[].[{}]Or foo[][{}]
|
Select [NBT Compound / JSON Object]compound elements in the lists in the [NBT List / JSON Array] foo list. | ||||
foo.bar.[0].[0].bazOr foo.bar[0][0].baz etc.
|
Select the baz tag in the first compound element in the first list element in the bar list in the [NBT Compound / JSON Object] foo tag. |
Examples
Name escaping
Quoted strings, such as "Lorem ipsum" or 'Lorem ipsum', can be used if a tag name needs to be escaped.
Mixed path
{}—Specifies the root tag{foo:4.0f}—Specifies the root tag if its children tag "foo" is4.0ffoo—Specifies the tag named "foo" under the root tagfoo.barorfoo{}.bar—Specifies the children tag named "bar" under the foo tag (should be a compound)foo.bar[0]—Specifies the first element of the "bar" tag (should be a list or array)foo.bar[-1]—Specifies the last element of the "bar" tag (should be a list or array)foo.bar[0]."A [crazy name]!"—Specifies the children tag named "A [crazy name]!" under that first element (should be a compound)foo.bar[0]."A [crazy name]!".baz—Specifies the children tag named "baz" under that crazily named tag (should be a compound)foo.bar[]—Specifies all elements of the "bar" tag (should be a list or array)foo.bar[].baz—Specifies the children tags named "baz" under all elements (should be compounds) of the "bar" tag (should be a list)foo.bar[{baz:5b}]—Specifies all elements (should be compounds) of the "bar" tag (should be a list), in which the "baz" tags are5bfoo{bar:"baz"}—Specifies the "foo" tag (should be a compound) if its children tag "bar" has the value"baz"foo{bar:"baz"}.bar—Specifies the "bar" tag under "foo" (should be a compound), only if it matches the value"baz"
Example 1
/data get entity @p foo.bar[0]."A [crazy name]!".baz
foo—Specifies the tag named "foo" under the root tag.foo.bar—Specifies the child tag named "bar" under the foo tag (should be a compound)foo.bar[0]—Specifies the first element of the "bar" (should be a list or array)foo.bar[0]."A [crazy name]!"—Specifies the child tag named "A [crazy name]!" under that first element (should be a compound)foo.bar[0]."A [crazy name]!".baz—Specifies the child named "baz" under that crazily named tag (should be a compound)
The tree structure
- [NBT Compound / JSON Object] The root entity data
- [NBT Compound / JSON Object] foo: The "foo" tag
- [NBT List / JSON Array] bar: The "bar" tag
- [NBT Compound / JSON Object] The first element of the list "bar"
- [NBT Compound / JSON Object] A [crazy name]!: The "A [crazy name]!" tag
- [Byte] baz: The "baz" tag; the target tag of this example.
- [NBT Compound / JSON Object] A [crazy name]!: The "A [crazy name]!" tag
- [NBT Compound / JSON Object] Another unrelated element in the list "bar"
- [NBT Compound / JSON Object] The first element of the list "bar"
- [NBT List / JSON Array] bar: The "bar" tag
- [NBT Compound / JSON Object] foo: The "foo" tag
Example 2
/data get block ~ ~ ~ Items[1].components.minecraft:written_book_content.pages[3].raw
A player has written a book and placed it inside a chest, and Alex is going to work up to the above command in stages. Observe the following imaginary chat log:
Chat log * Alex jumps on top of a chest. * Alex runs the command: /data get block ~ ~ ~ 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "minecraft:clock", count: 1}, {Slot: 9b, id: "minecraft:written_book", count: 1, components: {"minecraft:written_book_content": {pages: [{raw: '"twas brillig and the slithy toves"'}, {raw: '"Did gyre and gimble in the wabe."'}, {raw: '"All mimsy were the borogoves"'}, {raw: '"And the mome raths outgrabe."'}], author: "LewisCarroll", title: {raw: "Jabberwocky"}, resoveled: 1b}}}], id: "minecraft:chest"} * Alex only wants to look at the chest's inventory. They could search the Minecraft Wiki for what the name of that tag is, but because they understand how to read SNBT, they decide to figure it out from the command's output. * Alex runs the command: /data get block ~ ~ ~ Items 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "minecraft:clock", count: 1}, {Slot: 9b, id: "minecraft:written_book", count: 1, components: {"minecraft:written_book_content": {pages: [{raw: '"twas brillig and the slithy toves"'}, {raw: '"Did gyre and gimble in the wabe."'}, {raw: '"All mimsy were the borogoves"'}, {raw: '"And the mome raths outgrabe."'}], author: "LewisCarroll", title: {raw: "Jabberwocky"}, resoveled: 1b}}}], id: "minecraft:chest"} * Alex wants to narrow this down to the second item in the chest. Counting from 0, the second item would be element 1. * Alex runs the command: /data get block ~ ~ ~ Items[1] 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "minecraft:clock", count: 1}, {Slot: 9b, id: "minecraft:written_book", count: 1, components: {"minecraft:written_book_content": {pages: [{raw: '"twas brillig and the slithy toves"'}, {raw: '"Did gyre and gimble in the wabe."'}, {raw: '"All mimsy were the borogoves"'}, {raw: '"And the mome raths outgrabe."'}], author: "LewisCarroll", title: {raw: "Jabberwocky"}, resoveled: 1b}}}], id: "minecraft:chest"} * Alex wants just the "components" tag. * Alex runs the command: /data get block ~ ~ ~ Items[1].components 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "minecraft:clock", count: 1}, {Slot: 9b, id: "minecraft:written_book", count: 1, components: {"minecraft:written_book_content": {pages: [{raw: '"twas brillig and the slithy toves"'}, {raw: '"Did gyre and gimble in the wabe."'}, {raw: '"All mimsy were the borogoves"'}, {raw: '"And the mome raths outgrabe."'}], author: "LewisCarroll", title: {raw: "Jabberwocky"}, resoveled: 1b}}}], id: "minecraft:chest"} * Alex wants just the "pages" tag. * Alex runs the command: /data get block ~ ~ ~ Items[1].components.minecraft:written_book_content.pages 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "minecraft:clock", count: 1}, {Slot: 9b, id: "minecraft:written_book", count: 1, components: {"minecraft:written_book_content": {pages: [{raw: '"twas brillig and the slithy toves"'}, {raw: '"Did gyre and gimble in the wabe."'}, {raw: '"All mimsy were the borogoves"'}, {raw: '"And the mome raths outgrabe."'}], author: "LewisCarroll", title: {raw: "Jabberwocky"}, resoveled: 1b}}}], id: "minecraft:chest"} * Alex wants just the fourth element from this list. * Alex runs the command: /data get block ~ ~ ~ Items[1].components.minecraft:written_book_content.pages[3] 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "minecraft:clock", count: 1}, {Slot: 9b, id: "minecraft:written_book", count: 1, components: {"minecraft:written_book_content": {pages: [{raw: '"twas brillig and the slithy toves"'}, {raw: '"Did gyre and gimble in the wabe."'}, {raw: '"All mimsy were the borogoves"'}, {raw: '"And the mome raths outgrabe."'}], author: "LewisCarroll", title: {raw: "Jabberwocky"}, resoveled: 1b}}}], id: "minecraft:chest"} * Alex wants just the raw text in it. * Alex runs the command: /data get block ~ ~ ~ Items[1].components.minecraft:written_book_content.pages[3].raw 0 55 0 has the following block data: {x: 0, y: 55, z: 0, Items: [{Slot: 0b, id: "minecraft:clock", count: 1}, {Slot: 9b, id: "minecraft:written_book", count: 1, components: {"minecraft:written_book_content": {pages: [{raw: '"twas brillig and the slithy toves"'}, {raw: '"Did gyre and gimble in the wabe."'}, {raw: '"All mimsy were the borogoves"'}, {raw: '"And the mome raths outgrabe."'}], author: "LewisCarroll", title: {raw: "Jabberwocky"}, resoveled: 1b}}}], id: "minecraft:chest"} * Alex has what they need now, and uses the paths they have gathered to edit the book from outside the chest. * Alex runs the command: /data modify block ~ ~ ~ Items[1].components.minecraft:written_book_content.pages[3].raw set value '"And this pig here\'s named Babe."' Modified block data of 0 55 0 * Alex runs the command:' /data modify block ~ ~ ~ Items[1].components.minecraft:written_book_content.pages prepend value {raw: '"Call me Ishmael."'} Modified block data of 0 55 0 * Alex runs the command: /data modify block ~ ~ ~ Items[1].components.minecraft:written_book_content.author set value "Cthulhu the Sleeper"
History
| Java Edition | |||||||
|---|---|---|---|---|---|---|---|
| 1.20 | Pre-release 2 | Single quotation marks are now supported in NBT paths.[1] | |||||
References
| Versions | |||
|---|---|---|---|
| Development |
| ||
| Technical |
See here for more information | ||
| Game customization | |||