Resource icon
  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Supported Minecraft Versions
  1. 1.12
  2. 1.13
  3. 1.14
  4. 1.15
  5. 1.16
Please use skript-json over this if you can

Required Addons: SkQuery
Tested Versions: PaperSpigot 1.12.2, Skript 2.2-dev36, SkQuery 3.6.0-Lime

Parse JSON in Skript with minimal addons. A slow, inefficient, trash parser.

Usage:
Don't mess with functions named as 'JSON___f___()'
Use functions named as 'JSON_f()'

code_language.skript:
function JSON_parse(json: string, bufferAddress: string) :: string

Parses JSON from {_json} and outputs it to {%{_bufferAddress}%::*}
Returns error, if any.
Remember to clear the buffer if any error is given, as the function itself will not delete the parsed data before the error occured.

Some things to remember:
  • JSON 'null' is parsed as Skript 'NaN'
  • The type of {var::*} ("array" or "object") is stored in {var}, i.e.
    if JSON '{"hello": [2, 5]}' is parsed to variable "var", then
    {var} = "object"
    {var::hello} = "array"
    {var::hello::1} = 2
    {var::hello::2} = 5
  • Special characters cannot be parsed in Skript.
    '\n' and '\t' is acceptable and will be correctly parsed.
    All other characters (e.g. '\b', '\f', '\u00BB') will be converted to "<U+HHHH>" where H is the hex code for the symbol.
  • Skript list variable indexes are case-insensitive, so JSON keys can be overwritten. E.g.
    '{"Hello": 1, "hello": 2}'
    This might yield either {var::hello} = 1 or {var::hello} = 2
  • Skript list variable indexes have some restriction as to what characters can be used. For example, '*' asterik can only be used to denote a list variable '{var::*}', but can't be used as '{literal asterik *}', so JSON keys containing invalid variable keys have undefined results.

Example Code:
code_language.skript:
on load:
    set {_json} to "{""key1"":""a string"", ""key2!!"": 5}"
    set {_error} to JSON_parse({_json}, "-res")
    if {_error} is set:
        broadcast "oh no! an error happened:%nl%%{_error}%"
        delete {-res} and {-res::*}
        exit trigger
    broadcast "Parsed JSON!"
    broadcast "&a%{-res}%"
    loop {-res::*}:
        broadcast "  &b%loop-index% = %loop-value%"
    delete {-res} and {-res::*}

Output:
upload_2020-7-22_19-16-13.png

code_language.skript:
function JSON_serialize(jsonAddress: string) :: string
Recursively serializes (stringifies) JSON from {_jsonAddress} and returns it.

Some things to remember:
  • Serializing might break JSON if it involves empty objects/arrays. Empty objects/arrays will simply be serialized to the string "object" and "array" respectively, i.e.
    Parsing '[{}, []]' then serializing it will yield '["object", "array"]'
    Why?
    Skript has no way to represent empty containers. Currently, container differentiated by using strings "object" and "array" and then detecting if the address corresponds to a list variable.
  • This function is recursive. If the JSON is nested too deep, Skript may print "excessive or infinite repetitions" or similar to console and stop this function.
  • If the type of container is not specified in the given address it is taken as object by default. E.g.
    {var::1} = "hello"
    {var::2} = false
    Now, you might say this is an array because it has number indexes. But, nothing differentiates that from an object which simply has the strings "1" and "2" as its indexes. As an object can house any valid variable index in skript as its key, an object is chosen by default. To serialize as an array, you need to set
    {var} = "array"
Example Code:
code_language.skript:
on load:
    set {-var} to "array"
    set {-var::1} to 5
    set {-var::2} to NaN value
    set {-var::3} to "hiii"
    set {-var::4} to "object"
    set {-var::4::eyy} to "oii!"
    # Here, var must not be local as JSON_serialize needs to read from it
    broadcast JSON_serialize("-var")

Output:
upload_2020-7-22_19-29-59.png

code_language.skript:
# Note: UUID must be without hyphens/dashes.
function fetchLatestUsername(uuid: string) :: string:
    # Fetch data from Mojang API
    set {_data} to text from "https://api.mojang.com/user/profiles/%{_uuid}%/names"
    # An expression from SkQuery, returns a random UUID which is used as a temporary address for a buffer here.
    set {_bufferAddress} to new uuid
    # Call the parser
    set {_error} to JSON_parse({_data}, "-%{_bufferAddress}%")

    # Check for errors:
    if {_error} is set:
        # Clear the buffer! Always remember to do this, regardless of success.
        delete {-%{_bufferAddress}%} and {-%{_bufferAddress}%::*}
        send "Error while parsing JSON in %script%/fetchLatestUsername:%nl%%{_error}%" to console
        exit trigger
    
    # No errors! Getting our username in a local variable...
    set {_last} to size of {-%{_bufferAddress}%::*}
    set {_username} to {-%{_bufferAddress}%::%{_last}%::name}
    # Clear the buffer! Always remember to do this, regardless of success.
    delete {-%{_bufferAddress}%} and {-%{_bufferAddress}%::*}
    return {_username}
Author
CreeperOverLord7
Downloads
31
Views
552
First release
Last update
Rating
0.00 star(s) 0 ratings