Set or Change a JSON's Value
Sets or changes a specific value within a JSON object to a new value.
Overview
This procedure sets or changes a specific value within a JSON object to a new value. The verbs "set" and "change" are interchangeable. It supports both simple field names and nested paths using dot notation (e.g., settings.notifications.enabled). If the field does not exist, it will be created. The JSON object is modified in place.
Input
json
json
The JSON object to be modified
Yes
N/A
thing
string
The path within the JSON object where the value is to be set or changed (specified as a handle)
Yes
N/A
target
any
The new value to be set at the specified path within the JSON object
Yes
N/A
Output
This procedure modifies the JSON in place and does not return a value.
Examples
1. Setting a simple field value
This example sets the "age" field of the JSON object to 23.
get the json
the json's age is 232. Setting a field using the set command
This example sets the "name" field of the JSON object to "John Doe".
get the json
set the json's name to "John Doe"3. Setting a nested field using dot notation
This example sets a deeply nested field using dot notation.
get the json
change the json's "settings.notifications.enabled" to false4. Setting and modifying fields in sequence
This example shows multiple sequential modifications to JSON fields. The TermsCode is changed from "38" to 39.
the term is "{\"TermsCode\": \"38\", \"Description\": \"0.5% 10 NET 45\"}"
get the term as a json
the term is a json
get the term's TermsCode
get the term's Description
the term's TermsCode is 39
get the term's TermsCode
the term5. Using set command explicitly
This example uses the explicit "set" command to modify a field.
set the term's TermsCode to 41
get the term's TermsCode6. Using change command
This example uses the "change" command, which is equivalent to "set".
change the term's TermsCode to 42
get the term's TermsCode7. Adding a new field to a JSON object
This example adds a new field "Label" to an existing JSON object. The result would be: {"TermsCode": 42, "Description": "0.5% 10 NET 45", "Label": "Default"}
the term's Label is "Default"
the term8. Setting nested JSON structures
This example demonstrates setting one JSON object as a field within another JSON object.
the internaljson is "{}"
the internaljson is a json
set the internaljson's age to "23"
the finaljson is "{}"
the finaljson is a json
set the finaljson's data to the internaljson9. Setting a list as a field value
This example shows setting a list as a field value in a JSON object.
imagine valid items
the number is 1
add the number to the valid items
set the finaljson's name to the items10. Setting array element by ordinal
This example demonstrates setting array elements using ordinals like "first", "second", and "last".
set the foo's first apple to 10
set the foo's second apple to 20
set the foo's last apple to 4011. Setting nested array fields
This example shows setting fields within objects that are elements of an array.
set the foo's second objects.b to 22
set the foo's last objects.c to 33Last updated
Was this helpful?
