Minecraft Blogs

Function Data Packs for Dummies #2 | Who's JSON (and what's /tellraw)?

  • 25
  • 7
  • 4
avatar Bertiecrafter
Level 52 : Grandmaster Cyborg
118
This series of tutorials will teach you how to create your very own datapacks, assuming you know absolutely nothing about commands. Datapacks can contain a number of things, but only functions and tags will be covered in these posts. In every part I assume you've read the previous parts.

Where is JSON used?

No, it's not a person. It's a standardised definition language that allows passing what programmers call "objects" from one application to another in a human readable format. It's used all over the web. Since it was originally developed for use with JavaScript, JSON stands for "JavaScript Object Notation". In minecraft, it's used in technical files and any commands that change or display text. It's also a great start for understanding NBT. That's another definition language based on JSON, but only a tiny bit more difficult.

How do I write JSON?

JSON is just plain text written according to rules (syntax). The entirety of a JSON file is just 1 value. If you were to put a simple value in it, like a number, you can't put anything else in that JSON file. This is why the top-level value is often a complex one, allowing more values to be put inside. Each of the examples below is valid JSON.

The simple (primitive) value types are:
  • A number with a dot as decimal separator. Examples: 4 or 3.5
  • A string, also described as a piece of text surrounded by quotes. Example: "Hi, how are you doing?"
  • A boolean (case sensitive). Examples: true or false
  • Nothing. Keep in mind that this is different from 0, " " or false. This value is not used in Minecraft, it's just listed here for completeness. Example: null
There are also more complex values. The complex value types are:
  • Array: It's a list of values. These values could be of any type, but usually are of the same type. Each value is separated by a comma. You're not allowed to place a comma after the last value. Example: [
    "This is an array of values",
    50,
    true,
    null,
    [
    "Remember, an array is also a value"
    ]
    ]
  • Object or Compound: It's a collection of string keys and values. Once again, the values can be of any type, the key/value pairs are separated by commas and you can't have a comma behind the last pair. Example:{
    "my favourite number": 15,
    "my pet": {
    "name": "Max",
    "type": "dog"
    },
    "my favourite food": [
    "pizza",
    "cake"
    ]
    }

By combining complex values with simple values you can create an entire object structure full of information. The first all-containing value is called the "root".

Why do some types have multiple icons?

JavaScript does not differentiate between different kinds of numbers, which is why JSON (created for JavaScript) doesn't either. This isn't a problem for light pieces of code that JavaScript was made for. However, a complex piece of software like a game needs optimisations. By splitting up a "number" into different types with different ranges, less bytes can be used if they aren't needed. Carefully choosing the amount of bytes needed for a number can save both RAM and disk space.

Lucky for you, you don't have to worry about any of this. You just have to be able to understand the icons used by the Minecraft Wiki and the range limitations that go with them. The wiki pages might specify additional restrictions for each value in any JSON file. They're hard to miss if you ever read up on one though. Have a look here for an up to date list of value types. Ignore the different writing styles shown there, they don't apply to JSON. A number in JSON is always just a line of digits with possibly a dot somewhere in-between.

Icon NameRange

ByteWhole number from -128 to 127

ShortWhole number from -32,768 to 32,767

IntegerWhole number from -2,147,483,648 to 2,147,483,647

LongWhole number from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Double
These are the only types that allow decimals. A double is less precise than a float, which means a double can represent less numbers than a float within the same range. It's barely noticeable though, so just forget about it.
The ranges are also insane. ±34 followed by 37 zeros for a double and ±17 followed by 307 zeros for a float.
Float

Byte ArrayA list of bytes
Integer Array
Long Array
A list of either integers or longs. The wiki will tell you what kind of value is expected in a specific slot.

ListA list of anything, not just numbers. The wiki will tell you what's expected.

Don't try to remember all of them. Just be aware of the icons and , since you're likely to try a value outside their ranges if you're not careful.

Using JSON in the tellraw command

You might have heard of /say. It broadcasts a message but it doesn't have much functionality. The text is always white and it's always prefixed with the name of the executioner (a player name, "@" for command blocks and "Server" for functions/console). Tellraw allows you to print text messages in color, many different styles and you can even make it change its content based on certain conditions. It's one of the many places where JSON is used.

The syntax is simple: /tellraw <player> <json>
You can find the JSON structure here.
The icons you see represent the value types. Explanation of the icons can be found here.

According to the wiki the JSON is either a string, an array or an object. Which means that this is correct:
/tellraw <player> "Hey there!"
Hey there!

We didn't accomplish much with this though. Let's have a look at the object. If you look at where it says "The base chat component object", you'll see an icon in front. That icon means that it's an object. Below that line you have all the keys written out and the icons tell you the value type.
Because the root is an object, we must start our JSON structure with: /tellraw <player> {}
Now if we look at the "text" key you'll see that its icon is a page with lines. This means that it requires a string value. Using this information we can create this command: /tellraw <player> {"text":"Hey there!"}
Hey there!

Have a look at all the other possible keys. For example, you can see that the "bold" key requires a boolean. So if you want to display bold text, you'd use this:
/tellraw <player> {"text":"LOOK AT ME","bold":true}
LOOK AT ME

The style keys apply to the entire text in that object. If you want multiple styles in the same command, use the "extra" key (which is an array type). Note that all children inherit the styling of their parent unless overridden. Example:
/tellraw <player> {"text":"Base Text ","bold":true,"color":"red","extra":[
{"text":"Child Text ","color":"yellow"},
{"text":"Child Text"}
]}
Base Text Child Text Child Text

I don't recommend using the array version, because it doesn't behave the way it looks. Have a look at this command: /tellraw <player> [{"text":"hey","color":"dark_green"},{"text":" there"}]
hey there

You'd expect the second word to be white, since it has no color tag. However, the command is translated into:
/tellraw <player> {"text":"hey","color":"dark_green","extra":[{"text":" there"}]}
hey there

Which causes the second word to be green as well, since it inherits style properties from its parent. See how this can be confusing?

Lastly, you can use "\n" to insert new lines. This allows you to have multi-line /tellraw commands.
/tellraw {"text":"Hey there!\nHow are you doing?"}
Hey there!
How are you doing?


Challenge Yourself

After every tutorial, I'll include a section where you are challenged to apply what you've learned. I recommend you playing with what you've learned, it helps you getting familiar with new concepts and be able to find solutions to problems. You don't have to do exactly what's written below, you can always challenge yourself in a different way.

Try sending a hyperlink to yourself or a friend using /tellraw and the wiki page. The hyperlink must be colored aqua and underlined. If you hover over it, it should show a description (no style required). If you click it, it should open this website: https://theuselessweb.com/

If the command turns out to be too long for chat, you can give yourself a command block (/give @s command_block), put the command in there and slap a button on it.

What's next?

Next up we're going to create our first datapack. It won't do much yet, but you need to know the basics in order to do something awesome.
Subscribe if you want to get notified of new posts.

Tags

3 Update Logs

Update #3 : 04/06/2020 4:23:20 pmApr 6th

Added a little piece of text above the value types, explaining that the examples are not just parts of a bigger structure, they are complete and valid JSON. I also added Icons and a guide on how to read them, which comes in handy if you ever read up on a JSON file on a wiki page.
LOAD MORE LOGS

1
04/25/2020 9:49 am
Level 1 : New Miner
neanterrtall
hello we did exactly like you but the data pack does not launch
2
04/25/2020 2:04 pmhistory
Level 52 : Grandmaster Cyborg
Bertiecrafter
Try placing "say hi" in several function files, starting with the load or tick functions. If you see output, you know it's reaching those files. Once you know the functions are triggered at the right times, it's just a matter of getting the commands right. If you don't see any output, you either didn't call the function correctly, or the function has a syntax error causing the entire function to not load. Verify each function is error free by typing "/function " into chat and using the auto-complete to check if your function is there.
4
11/10/2019 1:42 pmhistory
Level 52 : Grandmaster Cyborg
Bertiecrafter
Thank you Cyprezz, MasterKiloRen99, Luracasmus, PMC, daitallica, Wundercroft, DinowCookie, Technodono, mouse36, TheBlindBat, flowfulfish, Sqperdoo, AstroVulpix, neanterrtall, Flyte_less , Masta969, Chimerabot, Rune_Bloodstone, ShadeOfLight, olmocap, Rhaast77, Gamer07_2, PommDeTerr and Nattehine for the diamonds!
2
01/29/2020 2:50 pmhistory
Level 64 : High Grandmaster Artist
Luracasmus
: D
Planet Minecraft Logo

Website

© 2010 - 2020
www.planetminecraft.com

Welcome