Skip to content

Modifying JSON

coffeeRequired edited this page May 9, 2025 · 7 revisions

This guide covers how to modify and manipulate JSON data in skJson.

Basic Modifications πŸ“

Setting Values

# 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"

Adding Values

# 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

Removing Values

# 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

Deleting Values

# 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]

Getting Values

# 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"

Removing All Values

# 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

Resetting Values

# 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

Advanced Operations πŸš€

Working with Arrays

# 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

Working with Objects

# 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}

Best Practices πŸ’‘

  1. Always validate data before modification
  2. Use meaningful path names
  3. Handle errors appropriately
  4. Keep JSON structure consistent
  5. Document complex modifications
  6. Use proper JSON formatting
  7. Handle null values appropriately

Examples πŸ“š

User Profile Update

# 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

Inventory Management

# 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]

Complex Structure Modification

# 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"

Common Issues and Solutions πŸ”

Invalid Path

# Check if path exists before modification
if {_json} has key "user":
    set {_json}.user.name to "John"
else:
    send "User path does not exist!"

Type Mismatch

# Ensure correct type
if json type of {_json}.age is json-primitive:
    set {_json}.age to 25
else:
    send "Age must be a number!"

Value Removal

# 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

Next Steps πŸ‘£

Note

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