Skip to content

Modifying JSON

cooffeeRequired edited this page Apr 23, 2025 · 7 revisions

Modifying JSON ๐Ÿ”ง

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

Basic Modifications ๐Ÿ“

Setting Values

# Set value using path
set {_json}.name to "John"
set {_json}.age to 25

# Set nested values
set {_json}.user.address.city to "New York"

# Set value using JSON path
set value of json path "user.address.city" in {_json} to "New York"

Adding Values

# Add to array
add "item1" to {_json}.items
add "item2" to {_json}.items

# Add to object
set {_json}.newField to "value"

# Add to JSON path
add "item" to json path "items" in {_json}

Removing Values

# Remove from array
remove "item1" from {_json}.items

# Remove from object
remove {_json}.oldField

# Remove using JSON path
remove value of json path "items.0" in {_json}

Advanced Operations ๐Ÿš€

Working with Arrays

# Access array elements
set {_first} to {_json}.items[0]
set {_last} to {_json}.items[-1]

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

JSON Path Operations ๐Ÿ—บ๏ธ

Using JSON Path

# Create JSON path
set {_path} to json path "user.address.city" in {_json}

# Add to path
add "newCity" to {_path}

# Set value using path
set value of json path "user.address.city" in {_json} to "New York"

Path Validation

# Check if path exists
if {_json} has values "user.address.city":
    send "Path exists!"

# Check if key exists in path
if {_json} has key "user.address.city":
    send "Key exists in path!"

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 value of json path "r[0].A" in {_json} to "classic set - array - nested"
set value of json path "r[1].c" in {_json} to "troll"
set value of json path "r[1].d" in {_json} to "troll 2"
set value of json path "r[1].e[0].x.b" in {_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.

Clone this wiki locally