Basic Knowledge of JSON
Author: XLuoxiany; Original Article: https://www.bilibili.com/read/cv15533561/Hello everyone, I'm Luoxian (Haotian). It's been a year since I last updated this column. Recently, for some reason, some of my previous articles have been dug up again. Mentioned in those articles was the JSON format beginner's tutorial by the "lemo" master, which unfortunately got deleted by the master. So, I decided to write a JSON format beginner's tutorial myself.
This tutorial mainly targets Minecraft Addons and commands, and is not limited to Minecraft—please read it as appropriate.
Applications of JSON in Minecraft:
JSON is used for various purposes in Minecraft, including NBT (Named Binary Tag) data used in commands, index files for texture mapping, and manifest files for texture and behavior packs. All of these utilize the JSON format.
JSON Syntax:
Objects: Objects in JSON are enclosed within curly braces
{}
. An object consists of one or more key-value pairs. A key-value pair consists of a string key, a colon, and a value. For example:{"string_key": "value"}
The key in a key-value pair must be a string, as the name implies. The value can be a number, string, boolean, array, or another dictionary (object). Multiple key-value pairs are separated by commas, like so:
{"string_key": "value", "114514": "1919810"}
Note: There is no comma after the last key-value pair.
JSON supports last-value assignment. If the same key is assigned multiple times, the compiler will issue a warning, but it won't affect the parsing of JSON. The last assignment of the key will be considered during parsing.
A valid JSON must be wrapped in a dictionary (object).
Numbers: JSON treats numbers similarly to most programming languages—it doesn't distinguish between integers and floating-point numbers.
{"114514": 1919810}
Strings: Strings are handled the same way as in other programming languages. However, in JSON, you can only use double quotes
""
as string markers.{"114514": "Hmm hmm hmm hmm hmm ahh ahh ahh ahh ahh ahh ahh"}
Booleans: JSON has three boolean values: true, false, and null. In standard JSON syntax, boolean values must be lowercase.
{"abc": true} {"dfe": false} {"ghi": null}
Arrays: Arrays are enclosed within square brackets
[]
. Items in an array are separated by commas,
. Each item in an array can be a string, number, boolean, object, or another array.{ "an_array": [ true, false, null, 114514, "1919810", ["another_array"], {"an_object": ""} ] }
Similar to objects, there is no comma after the last item in an array.
Comments: JSON does not have dedicated commenting methods. In fact, strictly speaking, JSON format does not support comments at all. In non-strict JSON syntax (or JSON5+), you can use
//
for single-line comments and/* */
for multi-line comments in your JSON document. However, these might cause errors in older versions.Remember the feature of multiple assignments to the same key? If a key is assigned multiple times, the compiler will issue a warning, but it won't affect the parsing of JSON. JSON parses based on the last assignment. Utilizing this feature, we can achieve "pseudo-comments":
{ "codepage": 1, "A": "I am a comment", "A": 114514 }
By assigning twice, you achieve the effect of a comment. Or if you find yellow wavy lines unattractive or confusing, you can use
"//"
as a string key to declare a key-value pair acting as a comment:{ "number": 1919810, "//": "I am a comment//" }
That's all for the content of JSON~ So, until next time in our next column! (Well, in the next life—just kidding.)
Last updated