Minecraft Blogs / Tutorial

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

  • 3,708 views, 29 today
  • 57
  • 19
  • 10
Bertiecrafter avatar Bertiecrafter
Site Moderator
Level 62 : High Grandmaster Cyborg
389
This series of tutorials will teach you how to create your very own data packs, assuming you know absolutely nothing about commands. Although data packs could just contain files that replace built-in assets like resource packs, these posts will focus on adding new features and mechanics to the game. 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:
  • Function Data Packs for Dummies #2 | Who's JSON (and what's /tellraw)?Function Data Packs for Dummies #2 | Who's JSON (and what's /tellraw)?Function Data Packs for Dummies #2 | Who's JSON (and what's /tellraw)? A number with a dot as decimal separator. Examples: 4Or3.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
Float
These are the only types that allow decimals. A float is less precise than a double, which means a float can represent less numbers than a double 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 float and ±17 followed by 307 zeros for a double.
Double

Byte ArrayA list of bytes
Integer ArrayA list of integers
Long ArrayA list of longs

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

4 Update Logs

Updated data type icons : 10/24/2020 7:06:44 amOct 24th, 2020

The wiki used new icons, so I made this blog display the updated versions.
LOAD MORE LOGS

Create an account or sign in to comment.

3
07/06/2021 3:05 am
Level 1 : New Miner
omegab0ss
omegab0ss avatar
OOH I always wanted o make datapacks I'm so excited, I have made simple datapacks like loot table changing thingies and custom crafting, but I'm so excited to take it to the next level
2
07/06/2021 2:19 pm
Level 62 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
Just take your time to learn though. Take enough breaks if you need to :P
2
05/16/2021 6:57 pm
Level 23 : Expert Waffle
weewo
weewo avatar
lol "Who's JSON" like Jason
1
07/21/2021 12:57 pm
Level 1 : New Miner
MinerSteve100
MinerSteve100 avatar
J-Son. Jay-Son. lol
1
01/31/2021 2:43 pm
Level 1 : New Miner
Bozzl
Bozzl avatar
This doesn't seem to work in MEE?
3
02/01/2021 1:25 am
Level 62 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
I can't help you without more information. What exactly isn't working, what have you tried and did you get any kind of error message?
2
04/25/2020 9:49 am
Level 1 : New Miner
neanterrtall
neanterrtall avatar
hello we did exactly like you but the data pack does not launch
6
04/25/2020 2:04 pmhistory
Level 62 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
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.
7
11/10/2019 1:42 pmhistory
Level 62 : High Grandmaster Cyborg
Bertiecrafter
Bertiecrafter avatar
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, Nattehine, sanonasu, SaneGamer, TofuChild36, Aysim, WallEtheRobot, Yeetabix420, samham, citharus, Vellaris, kingmeha, Bozzi, HanaTheBanana, MegaMushroomJack, jeanluccy, Cracrafteur, A_Bread_Boii, RoBoo1194, Drew1, EnderWizard, FriskTemmie, minegamer1824, Smilo16, Silabear, SUPERIONtheKnight, Flashbulb, OMAR4D, Bartyv, User3689236G, omegab0ss, MarTisey12, LieutenanT_T for the diamonds!
3
01/29/2020 2:50 pmhistory
Level 75 : Legendary Artist
Luracasmus
Luracasmus avatar
: D
Planet Minecraft

Website

© 2010 - 2021
www.planetminecraft.com

Welcome