-
Notifications
You must be signed in to change notification settings - Fork 4
Modifying JSON
coffeeRequired edited this page May 9, 2025
·
7 revisions
This guide covers how to modify and manipulate JSON data in skJson.
# Set value using path
set json value "x.y" of {_json} to 1
set json values "x.y" of {_json} to 2, 3 and 4
# Set using literal syntax
set {_json}.list to "[]"
set {_json}.object to "{}"
set {_json}.object.key to "value"
set {_json}.object.val to "value 2"
# Add to array using path
add 10 to json array "x.y.z[]" of {_json}
add 20 and 30 and "lol" to json array "x.y.z" of {_json}
# Add using literal syntax
add 40 and "K" to {_json}.list
add location(0, 0, 0) and location(0, 0, 1) to {_json}.list
add 10 to {_json}.object
# Remove from array using path
remove 10 from json array "x.y.z" of {_json}
remove "lol" from json array "x.y.z" of {_json}
# Remove using literal syntax
remove 1 and 40 from {_json}.list
remove "good" from {_json}.list
remove "value" and "value 2" from {_json}.object
# Delete using path
delete json value "x" of {_json}
delete json values "x.y" of {_json}
# Delete using literal syntax
delete {_json}.object
delete {_json}.list
delete {_json}.list[0]
# Get using path
set {_value} to json value "x.y" of {_json}
set {_values::*} to json values "x.y" of {_json}
# Get using literal syntax
send {_json}.list.0 #* -> 1
send {_json}.list.1 #* -> 2
send {_json}.list.2 #* -> "good"
send {_json}.list #* -> 1, 2, "good"
send {_json}.object.key #* -> "value"
# Remove all using path
remove all 1 from json values "x.y" of {_json}
remove all 2 and 3 from json values "x.y" of {_json}
# Remove all using literal syntax
remove all 1 from {_json}.list
remove all "value" from {_json}.object
# Reset using path
reset json array "x.y.z" of {_json}
reset json object "x.y" of {_json}
# Reset using literal syntax
reset {_json}.object
# Access array elements
set {_first} to {_json}.items[0]
# Get array size
set {_size} to json size of {_json}.items
# Add multiple values
add 1, 2 and 3 to {_json}.items
# Get all keys
set {_keys::*} to all keys of {_json}
# Check if key exists
if {_json} has key "name":
send "Name exists!"
# Remove multiple keys
remove key "key1" and "key2" from {_json}
- Always validate data before modification
- Use meaningful path names
- Handle errors appropriately
- Keep JSON structure consistent
- Document complex modifications
- Use proper JSON formatting
- Handle null values appropriately
# Update user profile
set {_json}.user.name to "John Doe"
set {_json}.user.age to 30
set {_json}.user.address.city to "London"
set {_json}.user.address.country to "UK"
add "admin" to {_json}.user.roles
# Add item to inventory
set {_item} to json from "{'id': '123', 'name': 'Sword', 'quantity': 1}"
add {_item} to {_json}.inventory.items
# Update item quantity
set {_json}.inventory.items[0].quantity to 5
# Remove item
remove {_json}.inventory.items[0]
# Create complex structure
set {_json} to json from "{r: [10, 20], a: {b: true}}"
# Modify nested structure
set json value "r[0].A" of {_json} to "classic set - array - nested"
set json value "r[1].c" of {_json} to "troll"
set json value "r[1].d" of {_json} to "troll 2"
set json value "r[1].e[0].x.b" of {_json} to "troll 3"
# Check if path exists before modification
if {_json} has key "user":
set {_json}.user.name to "John"
else:
send "User path does not exist!"
# Ensure correct type
if json type of {_json}.age is json-primitive:
set {_json}.age to 25
else:
send "Age must be a number!"
# Remove specific value
remove false from {_json}
# Remove all occurrences
remove all false from {_json}
# Remove from specific path
remove values true from {_json}.a
- Return to Working with Requests
- Check out Working with Cache
- Learn about Working with Files
- View Examples for more usage examples
- See Creating JSON
Note
For more information about Markdown formatting and alerts, see GitHub's discussion on Markdown alerts.