Understanding JSON

TIP

This is an appendix page. You can start the guide from the beginning here.

JSON is a simple format for writing text files, in a way that is understandable to both Humans and Computers. Bedrock uses .json files as the "language" of Add-Ons, so you will need a solid understand of how to read and write json! If you have never heard of JSON before, you are encouraged to read through this tutorial. It will teach you everything you need to know about writing valid JSON files.

Valid JSON

The important thing to remember when writing JSON is that it must be completely error free, or it won't work at all. Even one wrong character, or one extra comma will cause the entire file to fail. For this reason, it's super important you write valid JSON.

We can use an online tool called json lint to tell us whether our JSON is valid. Simply paste your code into the website, and press Validate JSON. You will get a response indicating whether your code is correct or not, as well as the location and type of any errors.

Data Structures

In JSON, data can be written in a number of formats. Each format is specialized for the kind of data it wants to represent. Here are the structures we have available:

NameExampleExplanation
String"hello!"Words, or characters. Requires quotes.
Int15A number. No quotes.
Float1.2A fractional number. No quotes.
BooltrueEither true or false. No quotes.

And now, in .json format:

json
{
  "my_string": "hello!",
  "my_int": 15,
  "my_float": 1.2,
  "my_bool": true
}
1
2
3
4
5
6

In addition to these simple structures, we also have access to two special structures. Special structures are used to nest other data together.

Arrays

Arrays are written as two square brackets []. They represent a list. We can put other data structures inside of the list. Each element of the list should be separated by a comma.

Some examples:

StructureComment
[1, 2, 3]A list of integers.
["Red", "blue"]A list of strings. Notice the quotes!

And now, in .json format:

json
{
   "my_ints": [1, 2, 3],
   "my_strings": ["Red", "blue"]
}
1
2
3
4

Objects

Objects are written as two curly-brackets {}. Objects are a special syntax which contains named data structures. The name is called a key, and the structure is called a value. The examples earlier in this page was a dictionary containing examples of the other data types.

This key-value syntax looks like this: "<key>": <any structure>. Notice the quotes around the key, and the colon.

Here is an example of an object, which contains a few key-value-pairs.

Copy
json
{
	"a_list_of_integers": [1, 2, 3],
	"is_json_cool": true
}
1
2
3
4

We need to separate each key-value pair with a comma.

We call the key-value pairs of an object as its child or as being inside the object.

JSON Structure

In Minecraft, JSON files always begin with an object, which you can remember is two curly brackets:{}. We call this the top level object. We write our code inside of this object, in the form of key-value pairs.

Here is an example of a simple json file, used for Minecraft addons:

Copy
json
{
	"format_version": "1.12.0",
	"animations": {
		"animation.car.wheel_spin": {
			"loop": true,
			"animation_length": 1.0,
			"bones": {
				"front_wheels": {
					"rotation": ["q.modified_distance_moved * -30", 0, 0]
				},
				"back_wheels": {
					"rotation": ["q.modified_distance_moved * -30", 0, 0]
				}
			}
		}
	}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Take a careful look at the format. You will see that the entire structure is built out the data-structures that we have already learned. If you want to practice your json skills, try to answer these questions:

  • How many keys are there in the top level object. Can you name them?
  • What is the value of format_version?
  • What kind of data is stored in the "loop" key? (string, boolean, etc)

Troubleshooting Examples

Here are a few examples, to help you understand feedback you might recieve on the discord or online. We tend to use technical jargon when talking about errors in JSON, so hopefully this section helps familizrize you with the terms:


You wrote: "format_version": 1.12

They said: "The value for format_version is the wrong type. It should be a string.

Remember that type means one of the structures: String, Int, Float, Array or Object. If we examine our code, we will see that we put format_version to a Float, instead of a String. We can fix this problem by adding quotes around the "1.12".


You wrote: [1 2 5 6]

They said: "Your array is missing commas."

Remember that array elements need to be separated by commas. Your array should look like this: [1, 2, 5, 6]


They said: "You accidentally put the format version inside your description. It should go outside at the top level".

This means that the key-value pair for "format_version" as a child of the description. You should copy/paste the key-value pair out from the description object, and place it at the top level.

Contributors