JSON Functions

Before you read this guide, you should be familiar with what JSON is and where and how it’s being used.
You can familiarize yourself with JSON by reading a tutorial on W3Schools.

This guide will utilize Character Escaping and the $optOff function.

$jsonParse

$jsonParse is the primary function used when working with JSON data.
This function parses a JSON string into an object which can then be used by other JSON functions.

Syntax

$jsonParse[JSON string]

Parameters

  • JSON string (Type: String || Flag: Required): The JSON string to parse into an object.

Examples

See examples further down the guide.

$json

$json function retrieves JSON values from a specified key in the current JSON object.

The $json function will return an empty string if the value is null, the key doesn’t exist, no $jsonParse or $jsonSet function was executed, or $jsonClear was executed.

Syntax

$json[Key;...]

Parameters

  • Key (Type: String || Flag: Required): The JSON key which will be retrieved.

Examples

Without Arrays

$nomention
$jsonParse[{
    "username": "NightNutSky",
    "tag": "6700",
    "identity": {
        "age": 16
    }
}]

Username: $json[username]
Tag: $json[tag]
Age: $json[identity;age] years old

With Arrays

$nomention
$jsonParse[{
    "computer": [{
        "apps": {
            "software": ["BlueStacks", "Krita", "Visual Studio Code"\],
            "games": ["GTA 5", "RDR 2", "CS:GO", "Cyberpunk 2077"\]
        }
    },{
        "cpu": "Intel",
        "gpu": "NVIDIA",
        "ram": "XPG"
    }\]
}]

Software: $json[computer;0;apps;software]
Games: $json[computer;0;apps;games]
Brands: CPU - $json[computer;1;cpu], GPU - $json[computer;1;gpu], RAM - $json[computer;1;ram]

$jsonSet

$jsonSet sets or replaces the value at the specified JSON key.

Syntax

$jsonSet[Key;...;Value]

Parameters

  • Key (Type: String || Flag: Required): The JSON key where the value will be set or replaced.
  • Value (Type: Integer, Bool, Float, String || Flag: Required): The value to set or replace with.

Example

$nomention
$jsonParse[{
    "username": "NightNutSky",
    "tag": "6700",
    "identity": {
        "age": 16
    }
}]

Username: $json[username]
Tag: $json[tag]
Age: $json[identity;age] years old

$jsonSet[username;Priyanuj]
$jsonSet[tag;2626]
$jsonSet[identity;age;19]

$optOff[Username: $json[username]
Tag: $json[tag]
Age: $json[identity;age] years old]

$jsonSetString

$jsonSetString function sets or replaces the value at the specified JSON key. Always sets the value as a string.

Syntax

$jsonSetString[Key;...;Value]

Parameters

  • Key (Type: String || Flag: Required): The JSON key where the value will be set or replaced.
  • Value (Type: String || Flag: Required): The value to set or replace with.

This function is recommended to be used mostly in economic-related commands. Why? The next example will explain it.

Example

  • $jsonSet

    $nomention
    $jsonParse[{}]
    
    $jsonSet[balance;$message]
    
    Balance key was set to: $json[balance]
    

  • $jsonSetString

    $nomention
    $jsonParse[{}]
    
    $jsonSetString[balance;$message]
    
    Balance key was set to: $json[balance]
    

If we set this value as a number manually, we’ll encounter issues.

$nomention
$jsonParse[{
    "balance": 788895455566645444567
}]

Balance key: $json[balance]

$nomention
$jsonParse[{
    "balance": "788895455566645444567"
}]

Balance key: $json[balance]

Therefore, we should set big numbers as strings.

$jsonUnset

$jsonUnset removes the value at the specified JSON key.
In short, the opposite of the $jsonSet function.

Syntax

$jsonUnset[Key;...]

Parameters

  • Key (Type: String || Flag: Required): The JSON key which will be unset.

Example

$nomention
$jsonParse[{
    "username": "NightNutSky",
    "tag": "6700",
    "identity": {
        "age": 16
    }
}]

Username: $json[username]
Tag: $json[tag]
Age: $json[identity;age] years old

$jsonUnset[username]
$jsonUnset[tag]
$jsonUnset[identity;age]

$optOff[Username: $json[username]
Tag: $json[tag]
Age: $json[identity;age] years old]

$jsonClear

$jsonClear clears out the current JSON object.

Syntax

$jsonClear

Example

$nomention
$jsonParse[{
    "username": "NightNutSky",
    "tag": "6700",
    "identity": {
        "age": 16
    }
}]

Username: $json[username]
$jsonClear
Username: $optOff[$json[username]]

$jsonExists

$jsonExists checks if the specified JSON key exists in the current JSON object.

Returns an empty result if no $jsonParse or $jsonSet function was executed, or $jsonClear was executed.

Syntax

$jsonExists[Key;...]

Parameters

  • Key (Type: String || Flag: Required): The JSON key which will be checked.

Examples

See examples further down the guide.

$jsonStringify

$jsonStringify turns the current JSON object into a string value.

The $jsonStringify function will return an empty string if no $jsonParse or $jsonSet function was executed, or $jsonClear was executed.

Syntax

$jsonStringify

Example

$nomention
$jsonParse[{
    "computer": [{
        "apps": {
            "software": ["BlueStacks", "Krita", "Visual Studio Code"\],
            "games": ["GTA 5", "RDR 2", "CS:GO", "Cyberpunk 2077"\]
        }
    },{
        "cpu": "Intel",
        "gpu": "NVIDIA",
        "ram": "XPG"
    }\]
}]

$jsonStringify

$jsonPretty

$jsonPretty turns the current JSON object into a pretty string value.

The $jsonPretty function will return an empty result if no $jsonParse or $jsonSet function was executed, or $jsonClear was executed.

Syntax

$jsonPretty[Indent length]

Parameters

  • Indent length (Type: Integer || Flag: Required): The length of the indentation. Usually it’s 2 or 4.

Example

$nomention
$disableInnerSpaceRemoval
$jsonParse[{"computer":[{"apps":{"software":["BlueStacks","Krita","Visual Studio Code"\],"games":["GTA 5","RDR 2","CS:GO","Cyberpunk 2077"\]}},{"cpu":"Intel","gpu":"NVIDIA","ram":"XPG"}\]
}]

$jsonPretty[4]

For the output to really be pretty, we have to use the $disableInnerSpaceRemoval function.

$jsonArray

$jsonArray marks a specified JSON key as an array.

Syntax

$jsonArray[Key;...]

Parameters

  • Key (Type: String || Flag: Required): The JSON key which will be marked as an array.

Example

$nomention
$jsonParse[{
    "games": ""
}]

Non-array `games` key:
$jsonPretty[4]

$jsonArray[games]

Array `games` key:
$optOff[$jsonPretty[4]]

$jsonArrayCount

$jsonArrayCount counts the elements in the specified JSON key.

Syntax

$jsonArrayCount[Key;...]

Parameters

  • Key (Type: String || Flag: Required): The JSON key where the elements will be counted.

Example

$nomention
$jsonParse[{
    "computer": [{
        "apps": {
            "software": ["BlueStacks", "Krita", "Visual Studio Code"\],
            "games": ["GTA 5", "RDR 2", "CS:GO", "Cyberpunk 2077"\]
        }
    },{
        "cpu": "Intel",
        "gpu": "NVIDIA",
        "ram": "XPG"
    }\]
}]

$onlyIf[$jsonExists[computer;0;apps;$message]==true;The specified category doesn't exist! Available categories are `software` and `games`]
The count of the `$message` apps is $jsonArrayCount[computer;0;apps;$message]. 

$jsonArrayIndex

$jsonArrayIndex gets the array index of a given value.

The $jsonArrayIndex function will return -1 if value not found and will return an empty result if no $jsonParse or $jsonSet function was executed, or $jsonClear was executed.

Syntax

$jsonArrayIndex[Key;...;Value]

Parameters

  • Key (Type: String || Flag: Required): The JSON key where the value will be searched for.
  • Value (Type: String, Integer, Float || Flag: Required): The value to search for.

Example

$nomention
$jsonParse[{
    "computer": [{
        "apps": {
            "software": ["BlueStacks", "Krita", "Visual Studio Code"\],
            "games": ["GTA 5", "RDR 2", "CS:GO", "Cyberpunk 2077"\]
        }
    },{
        "cpu": "Intel",
        "gpu": "NVIDIA",
        "ram": "XPG"
    }\]
}]

The $message's index in `apps/software` is $jsonArrayIndex[computer;0;apps;software;$message].

$jsonArrayAppend

$jsonArrayAppend appends the value at the end of the specified JSON key.

Syntax

$jsonArrayAppend[Key;...;Value]

Parameters

  • Key (Type: String || Flag: Required): The JSON key where the value will be appended.
  • Value (Type: Integer, Bool, Float, String || Flag: Required) : The value to append.

Example

$nomention
$jsonParse[{
    "computer": [{
        "apps": {
            "software": ["BlueStacks", "Krita", "Visual Studio Code"\],
            "games": ["GTA 5", "RDR 2", "CS:GO", "Cyberpunk 2077"\]
        }
    },{
        "cpu": "Intel",
        "gpu": "NVIDIA",
        "ram": "XPG"
    }\]
}]

$onlyIf[$jsonExists[computer;0;apps;$message[1]]==true;The specified category doesn't exist! Available categories are `software` and `games`]

$var[value;$replaceText[$message;$message[1] ;]]

$jsonArrayAppend[computer;0;apps;$message[1];$var[value]]

A new app was added to the `$message[1]` category!
Current apps in the `$message[1]` category: $json[computer;0;apps;$message[1]]

$jsonArrayPop

$jsonArrayPop function removes the last element of an array and returns the removed element.

Syntax

$jsonArrayPop[Key;...]

Parameters

  • Key (Type: String || Flag: Required): The key of the array from which an element will be removed.

Example

$nomention
$jsonParse[{
    "music": ["Paranoid - MADKID", "Ping! 2 - Exyl", "Tokyo - Leat'eq"\]
}]

Removed: $jsonArrayPop[music]

Current music:
> $jsonJoinArray[music;, ]

$jsonArrayShift

$jsonArrayShift function removes the first element of an array and returns the removed element.

Syntax

$jsonArrayShift[Key;...]

Parameters

  • Key (Type: String || Flag: Required): The key of the array from which an element will be removed.

Example

$nomention
$jsonParse[{
    "music": ["Paranoid - MADKID", "Ping! 2 - Exyl", "Tokyo - Leat'eq"\]
}]

Removed: $jsonArrayShift[music]

Current music:
> $jsonJoinArray[music;, ]

$jsonArrayUnshift

$jsonArrayUnshift function adds the value in the front of the array.

Syntax

$jsonArrayUnshift[Key;...;Value]

Parameters

  • Key (Type: String || Flag: Required): The JSON key of the array to which the value will be added.
  • Value (Type: Float, String, Bool, Integer || Flag: Required): The value to be added.

Example

$nomention
$jsonParse[{
    "music": ["Paranoid - MADKID", "Ping! 2 - Exyl", "Tokyo - Leat'eq"\]
}]

$jsonArrayUnshift[music;Side Character - Cloudfodder]

Music:
> $jsonJoinArray[music;, ]

$jsonArraySort

$jsonArraySort sorts a specific JSON array in ascending order.

The function sorts the elements in the order of integers from lowest to highest, and then strings based on their ASCII/Unicode values.

Syntax

$jsonArraySort[Key;...]

Parameters

  • Key (Type: String || Flag: Emptiable): The key of the JSON array to be sorted.

Example

$nomention
$jsonParse[{
  "data": ["Oranges", "banana", 10, "apple", "Apples", 2, 30\]
}]

$jsonArraySort[data]

After sorting:
> $json[data]

$jsonArrayReverse

$jsonArrayReverse reverses the order of a specific JSON array.

Syntax

$jsonArrayReverse[key;...]

Parameters

  • Key (Type: String || Flag: Emptiable): The key of the JSON array to be reversed.

Example

$nomention
$jsonParse[{
  "fruits": ["apple", "orange", "banana", "grape"\]
}]

$jsonArrayReverse[fruits]

After reversing:
> $json[fruits]

$jsonJoinArray

$jsonJoinArray function joins a JSON array under the specified key with the given separator.

The $jsonJoinArray function will return an empty string if the value is null, the key doesn’t exist, no $jsonParse or $jsonSet function was executed, or $jsonClear was executed.

Syntax

$jsonJoinArray[Key;...;Separator]

Parameters

  • Key (Type: String || Flag: Required): The JSON key to an array which will be retrieved.
  • Separator (Type: String || Flag: Required): The separator which will separate the JSON keys.

Example

$nomention
$jsonParse[{
    "music": ["Paranoid - MADKID", "Ping! 2 - Exyl", "Tokyo - Leat'eq"\]
}]

Music:
> $jsonJoinArray[music;, ]

We separated the list with , .