Skip to content

Examples

cooffeeRequired edited this page May 9, 2025 · 4 revisions

Examples πŸ“š

This guide provides comprehensive examples of using skJson in various scenarios.

Basic JSON Operations πŸ”§

Creating and Modifying JSON

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

# Modify values
set {_json}.name to "Jane"
set {_json}.age to 30

# Add new fields
set {_json}.city to "New York"
add "admin" to {_json}.roles

Working with Arrays

# Create array
set {_json} to json from "{'items': ['apple', 'banana', 'orange']}"

# Add to array
add "grape" to {_json}.items

# Remove from array
remove "banana" from {_json}.items

# Access array elements
set {_first} to {_json}.items[0]
set {_last} to {_json}.items[-1]

File Operations πŸ“

Creating and Managing Files

# Create new file
create json file "data.json" and write to it "{'version': '1.0'}"

# Bind file to storage
bind json file "data.json" as "my-storage"

# Save changes
save json storage id "my-storage"

File Watchers

# Set up watcher
bind storage watcher to "my-storage"

# Handle file changes
on json watcher file change:
    if event-uuid is "my-storage":
        send "File has been updated!"

Cache Management πŸš€

Virtual Storage

# Create virtual storage
create json virtual storage named "temp-data"

# Store data
set {_data} to json from "{'temp': 'value'}"
set json storage of id "temp-data" to {_data}

# Access cached data
set {_cached} to json storage of id "temp-data"

Cache Mapping

# Create complex JSON
set {_json} to json from "{'users': [{'name': 'John'}, {'name': 'Jane'}]}"

# Map to variables
map {_json} to {_mapped::*}

# Access mapped data
loop {_mapped::*}:
    send "User: %loop-value%"

HTTP Requests 🌐

Basic Requests

# Create GET request
set {_request} to prepare GET request on "https://api.example.com/data"

# Execute request
execute {_request}

# Handle response
set {_response} to {_request}'s response
if status of {_response} is "OK":
    send body of {_response}

Advanced Requests

# Create POST request with headers and body
set {_request} to prepare POST request on "https://api.example.com/create"
set {_request}'s headers to "{'Content-Type': 'application/json'}"
set {_request}'s body to json from "{'name': 'John'}"

# Execute asynchronously
execute {_request} as non blocking

# Handle response in event
on http response received:
    if status of event-response is "OK":
        send "Data created successfully!"

Complex Examples πŸ“

User Management System

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

# Save to file
create json file "plugins/Skript/users/%player's uuid%.json" and write to it {_profile}

# Bind to storage
bind json file "plugins/Skript/users/%player's uuid%.json" as "user-%player's uuid%"

# Watch for changes
bind storage watcher to "user-%player's uuid%"

Configuration System

# Create config
set {_config} to json from "{'debug': false, 'maxPlayers': 100, 'features': ['chat', 'inventory']}"

# Save to file
create json file "plugins/Skript/config.json" and write to it {_config}

# Bind with watcher
bind json file "plugins/Skript/config.json" as "config" and let bind storage watcher

# Handle config changes
on json watcher file change:
    if event-uuid is "config":
        send "Configuration has been updated!"

Best Practices πŸ’‘

  1. Always validate JSON before using
  2. Use meaningful variable names
  3. Handle errors appropriately
  4. Keep file paths consistent
  5. Implement proper cache management
  6. Use appropriate HTTP methods
  7. Document complex operations

Common Patterns πŸ”„

Data Validation

# Check JSON type
if json type of {_json} is json-object:
    send "Valid JSON object"
else:
    send "Invalid JSON format"

# Check for required fields
if {_json} has keys "name" and "age":
    send "Required fields present"
else:
    send "Missing required fields"

Error Handling

# File operations
if json file "data.json" exists:
    set {_json} to json from file "data.json"
else:
    send "File not found!"

# HTTP requests
if status of {_response} is "OK":
    process response
else:
    send "Request failed: %status code of {_response}%"

Next Steps πŸ‘£

Note

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