-
Notifications
You must be signed in to change notification settings - Fork 3
Working with Files
cooffeeRequired edited this page May 9, 2025
·
4 revisions
This guide covers how to work with JSON files using skJson.
# Create empty JSON file
create json file "data.json"
# Create file with content
create json file "data.json" and write to it "{'name': 'John'}"
# Create with configuration
create json file "data.json" with configuration[replace=true, encoding=UTF-8]
# Bind JSON file to a storage ID
bind json file "data.json" as "my-storage"
# Bind with watcher
bind json file "data.json" as "my-storage" and let bind storage watcher
# Unbind storage
unbind json storage id "my-storage"
# Bind storage watcher
bind storage watcher to "my-storage"
# Unbind storage watcher
unbind storage watcher from "my-storage"
on json watcher file change:
set {_file} to event-file
set {_uuid} to event-uuid
set {_content} to event-json
send "File %{_file}% has been changed!"
if json file "data.json" exists:
send "File exists!"
# Get all JSON files in a directory
set {_files::*} to all json files from dir "plugins/Skript/data"
# Get file path from watcher event
on json watcher file change:
set {_path} to event-file
send "Changed file: %{_path}%"
- Always use meaningful storage IDs
- Implement proper error handling
- Use file watchers for real-time updates
- Keep file paths consistent
- Use appropriate file encoding
- Handle file permissions correctly
- Backup important files regularly
# Create player data file
create json file "plugins/Skript/playerdata/%player's uuid%.json"
# Bind to storage
bind json file "plugins/Skript/playerdata/%player's uuid%.json" as "player-%player's uuid%"
# Save player data
set {_data} to json from "{'name': '%player%', 'lastLogin': '%now%'}"
set json storage of id "player-%player's uuid%" to {_data}
save json storage id "player-%player's uuid%"
# Create config file
create json file "plugins/Skript/config.json" and write to it "{'debug': false, 'maxPlayers': 100}"
# Bind with watcher
bind json file "plugins/Skript/config.json" as "config" and let bind storage watcher
# Watch for changes
on json watcher file change:
if event-uuid is "config":
send "Configuration has been updated!"
# List all JSON files in directory
set {_files::*} to all json files from dir "plugins/Skript/data"
# Process each file
loop {_files::*}:
set {_json} to json from file loop-value
if json type of {_json} is json-object:
send "Processing file: %loop-value%"
# Always check if file exists
if json file "data.json" exists:
set {_json} to json from file "data.json"
else:
send "File not found!"
# Make sure the plugin has write permissions
create json file "data.json" with configuration[replace=true]
# Specify encoding when creating file
create json file "data.json" with configuration[encoding=UTF-8]
- Learn about Working with Cache
- Explore Working with Requests
- See how to Modify JSON
- Check out Examples for more usage examples
- Return to Creating JSON
Note
For more information about Markdown formatting and alerts, see GitHub's discussion on Markdown alerts.