Back to Home
$jsonUnset
$jsonUnset removes a key and its value from the current
JSON object. It is the opposite of $jsonSet.
Syntax
$jsonUnset[Key;...]
-
Key: one or more keys forming the path to the key to remove.
What happens
$jsonParseloads the JSON object into memory.-
$jsonUnsetfinds the key at the given path and removes it entirely. -
Nothing is returned. After this, calling
$jsonon that key returns an empty string.
Example 1: Remove a top-level key
$nomention
$jsonParse[{"username":"Zubariel","tag":"0001"}]
Before: $json[username]
$jsonUnset[username]
After: $json[username]
What happens:
$jsonParseloads the object.$json[username]readsZubariel.$jsonUnset[username]removes the key.$json[username]now returns empty.
Output:
Before: Zubariel
After:
Example 2: Remove a nested key
$nomention
$disableInnerSpaceRemoval
$jsonParse[{"user":{"name":"Zubariel","score":100}}]
$jsonUnset[user;score]
$jsonPretty[2]
What happens:
$jsonParseloads the object.-
$jsonUnset[user;score]removesscorefrom insideuser. $jsonPretty[2]shows the updated object.
Output:
{
"user": {
"name": "Zubariel"
}
}
Common uses
- Cleaning up objects: remove fields before saving or sending a JSON object
-
Resetting a key: remove a key so it can be
recreated fresh with
$jsonSet
See also
- $jsonSet: to set or update a value instead of removing it
- $jsonClear: to remove the entire object instead of just one key
- $jsonExists: to check if a key exists before unsetting it