-
Notifications
You must be signed in to change notification settings - Fork 4
Modifying JSON
cooffeeRequired edited this page Apr 23, 2025
·
7 revisions
This guide covers how to modify and manipulate JSON data in skJson.
# 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"
# 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}
# 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}
# 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
# 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}
# 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"
# 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!"
- 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 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"
# 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.