JSON Processing
When you need to access bigger or more complex sets of data that don't make sense to store in the source code, or co-processing with the output from other applications, JSON has you covered. MCCompiled features support for simple JSON processing and data traversal using a simple feature-set.
Loading JSON
Getting the JSON file into your project requires using the $json
command. The simplest syntax available is $json [file] [result]
, where file
is the file to load, and result
is the preprocessor variable identifier to store the JSON in.
Specifying Load Path
There is an optional parameter at the end of the $json
command called "path." The path tells MCCompiled where to traverse the JSON before loading it into the preprocessor variable. As an example, take the following JSON file:
If you didn't care about the version and just wanted to access the object "data," then using a path would make sense.
Path Format
Paths are basic and lax by design. A path is a string made up of "elements," separated by either a period .
or either type of slash: /
or \
. Each element in a path denotes where next to traverse the JSON. If you encounter a JSON array, you need to specify an integer rather than a string.
With the example text above, to fetch the element "data" you would specify the path:
To get the first element of "owners":
Indexing JSON
Now that you have loaded the JSON, you have a preprocessor variable containing either a JSON object or array. From here, you are free to index this JSON further or dereference the preprocessor variable to inlay the data in your code, just like a regular preprocessor variable.
Indexing is done using the regular index operator. Most importantly, an indexing operation must start with a $
to indicate that you wish to dereference and then index the result. Without a dereference, indexing a preprocessor variable will just return an element of it.
Chaining Indexers
You can also chain indexers together (still only requiring dereferencing at the start) to traverse the JSON more than one step at a time. Consider the example above, but without using a path when loading it.
Indexing Arrays
When indexing a JSON array, it must be indexed using an integer, since there are no named keys to go off. Integers must be specified without quotation marks, as with the rest of the language.