-
Notifications
You must be signed in to change notification settings - Fork 3
Examples
cooffeeRequired edited this page May 9, 2025
·
4 revisions
This guide provides comprehensive examples of using skJson in various scenarios.
# 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
# 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]
# 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"
# 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!"
# 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"
# 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%"
# 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}
# 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!"
# 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%"
# 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!"
- Always validate JSON before using
- Use meaningful variable names
- Handle errors appropriately
- Keep file paths consistent
- Implement proper cache management
- Use appropriate HTTP methods
- Document complex operations
# 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"
# 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}%"
- Learn about Creating JSON
- Explore Working with Files
- Check out Working with Cache
- See Working with Requests
- Understand Modifying JSON
Note
For more information about Markdown formatting and alerts, see GitHub's discussion on Markdown alerts.