1. 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!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

API JSON Parser 1.0

A small script with functions allowing you to parse JSON

  1. CreeperOverLord7
    Supported Minecraft Versions:
    • 1.12, 1.13, 1.14, 1.15, 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.

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

    Code (Skript):
    1. 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 (Skript):
    1. on load:
    2.     set {_json} to "{""key1"":""a string"", ""key2!!"": 5}"
    3.     set {_error} to JSON_parse({_json}, "-res")
    4.     if {_error} is set:
    5.         broadcast "oh no! an error happened:%nl%%{_error}%"
    6.         delete {-res} and {-res::*}
    7.         exit trigger
    8.     broadcast "Parsed JSON!"
    9.     broadcast "&a%{-res}%"
    10.     loop {-res::*}:
    11.         broadcast "  &b%loop-index% = %loop-value%"
    12.     delete {-res} and {-res::*}

    Code (Skript):
    1. 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"]'
      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 (Skript):
    1. on load:
    2.     set {-var} to "array"
    3.     set {-var::1} to 5
    4.     set {-var::2} to NaN value
    5.     set {-var::3} to "hiii"
    6.     set {-var::4} to "object"
    7.     set {-var::4::eyy} to "oii!"
    8.     # Here, var must not be local as JSON_serialize needs to read from it
    9.     broadcast JSON_serialize("-var")

    Code (Skript):
    1. # Note: UUID must be without hyphens/dashes.
    2. function fetchLatestUsername(uuid: string) :: string:
    3.     # Fetch data from Mojang API
    4.     set {_data} to text from "https://api.mojang.com/user/profiles/%{_uuid}%/names"
    5.     # An expression from SkQuery, returns a random UUID which is used as a temporary address for a buffer here.
    6.     set {_bufferAddress} to new uuid
    7.     # Call the parser
    8.     set {_error} to JSON_parse({_data}, "-%{_bufferAddress}%")
    10.     # Check for errors:
    11.     if {_error} is set:
    12.         # Clear the buffer! Always remember to do this, regardless of success.
    13.         delete {-%{_bufferAddress}%} and {-%{_bufferAddress}%::*}
    14.         send "Error while parsing JSON in %script%/fetchLatestUsername:%nl%%{_error}%" to console
    15.         exit trigger
    17.     # No errors! Getting our username in a local variable...
    18.     set {_last} to size of {-%{_bufferAddress}%::*}
    19.     set {_username} to {-%{_bufferAddress}%::%{_last}%::name}
    20.     # Clear the buffer! Always remember to do this, regardless of success.
    21.     delete {-%{_bufferAddress}%} and {-%{_bufferAddress}%::*}
    22.     return {_username}