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.

JSON Parser

Discussion in 'Scripts' started by CreeperOverLord7, Jul 22, 2020.

  1. CreeperOverLord7

    Joined:
    Jan 19, 2018
    Messages:
    17
    Likes Received:
    1
    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 (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::*}
    Output:
    View attachment 5106

    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"]'
      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 (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")
    Output:
    View attachment 5107
     

Share This Page

Loading...