-
Notifications
You must be signed in to change notification settings - Fork 4
Creating JSON
cooffeeRequired edited this page May 9, 2025
·
5 revisions
This guide will show you how to create and work with JSON data in your Skript scripts using skJson.
# 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'}}}"
# 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
# Create from a website
set {_json} to json from website "https://api.example.com/data.json"
# Create from a file
set {_json} to json from file "plugins/Skript/data.json"
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.
# 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}
# Print JSON with formatting
send {_json} as pretty print
# Print without colors
send {_json} as uncolored pretty print
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"
if {_json} is empty:
send "JSON is empty"
# 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!"
# 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
# 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
- Always validate JSON before using it
- Use pretty print for debugging
- Handle errors appropriately
- Use meaningful variable names
- Comment your code
- Use proper JSON formatting
- Handle null values appropriately
set {_profile} to json from "{'name': '%player%', 'uuid': '%player's uuid%', 'lastLogin': '%now%'}"
set {_inventory} to json from player's inventory
set {_inventory}.owner to player's name
set {_inventory}.timestamp to now
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%"
# Wrong
set {_json} to json from "{name: John}" # Missing quotes
# Correct
set {_json} to json from "{'name': 'John'}"
# 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}
- Learn about Working with Files
- Explore Working with Cache
- Check out Working with Requests
- See how to Modify JSON
- View Examples for more usage examples
Note
For more information about Markdown formatting and alerts, see GitHub's discussion on Markdown alerts.