Skip to content

Creating JSON

cooffeeRequired edited this page May 9, 2025 · 5 revisions

Creating JSON in Skript 🎨

This guide will show you how to create and work with JSON data in your Skript scripts using skJson.

Basic JSON Creation 📝

From String

# Create JSON from a string
set {_json} to json from "{'name': 'John', 'age': 25}"

# Create with nested structure
set {_json} to json from "{'user': {'name': 'John', 'age': 25, 'address': {'city': 'New York'}}}"

From Objects

# Create from Skript objects
set {_json} to json from player's location
set {_json} to json from player's inventory
set {_json} to json from diamond tools

From Website

# Create from a website
set {_json} to json from website "https://api.example.com/data.json"

From File

# Create from a file
set {_json} to json from file "plugins/Skript/data.json"

Working with JSON Data 🔧

Important

Conflict:

  • Skript-reflect: if you use skript-reflect, don't use the [] access for the field, but wrap the number for the example. {_array}.0 == {_array}[0]
  • SkBee - SkJson is now set to be loaded before SkBee to prevent conflicts.

Accessing Values

# Get values using path
set {_name} to value "name" of {_json}
set {_age} to value "age" of {_json}

# Get multiple values
set {_values::*} to values "items" of {_json}

# Get all keys
set {_keys::*} to all keys of {_json}

Pretty Printing

# Print JSON with formatting
send {_json} as pretty print

# Print without colors
send {_json} as uncolored pretty print

JSON Types and Validation ✅

Checking JSON Type

if json type of {_json} is json-object:
    send "This is a JSON object"
else if json type of {_json} is json-array:
    send "This is a JSON array"
else if json type of {_json} is json-primitive:
    send "This is a primitive value"
else if json type of {_json} is json-null:
    send "This is null"

Checking if Empty

if {_json} is empty:
    send "JSON is empty"

Checking Values and Keys

# Check if JSON has specific values
if {_json} has values 1 and 3:
    send "Values exist!"

# Check if JSON has specific keys
if {_json} has keys "name" and "age":
    send "Keys exist!"

Sorting JSON Data 📊

Sorting by Key

# Sort in ascending order by key
set {_sorted} to {_json} in ascending order by key

# Sort in descending order by key
set {_sorted} to {_json} in descending order by key

Sorting by Value

# Sort in ascending order by value
set {_sorted} to {_json} in ascending order by value

# Sort in descending order by value
set {_sorted} to {_json} in descending order by value

Best Practices 💡

  1. Always validate JSON before using it
  2. Use pretty print for debugging
  3. Handle errors appropriately
  4. Use meaningful variable names
  5. Comment your code
  6. Use proper JSON formatting
  7. Handle null values appropriately

Examples 📚

Creating a Player Profile

set {_profile} to json from "{'name': '%player%', 'uuid': '%player's uuid%', 'lastLogin': '%now%'}"

Creating an Inventory Snapshot

set {_inventory} to json from player's inventory
set {_inventory}.owner to player's name
set {_inventory}.timestamp to now

Creating from Website Data

set {_json} to json from website "https://api.example.com/users"
if json type of {_json} is json-array:
    loop values of {_json}:
        send "User: %json-value%"

Common Issues and Solutions 🔍

Invalid JSON Format

# Wrong
set {_json} to json from "{name: John}" # Missing quotes

# Correct
set {_json} to json from "{'name': 'John'}"

Accessing Nested Values

# For nested objects
set {_value} to value "user.address.city" of {_json}

# Using JSON path
set {_value} to value of json path "user.address.city" in {_json}

Next Steps 👣

Note

For more information about Markdown formatting and alerts, see GitHub's discussion on Markdown alerts.