Tcl
Tcl
Modify Parts of JSON Document
See more JSON Examples
Demonstrates how to modify parts of a JSON document. This example uses the following JSON document:
{
"fruit": [
{
"kind": "apple",
"count": 24,
"fresh": true,
"extraInfo": null,
"listA": [ "abc", 1, null, false ],
"objectB": { "animal" : "monkey" }
},
{
"kind": "pear",
"count": 18,
"fresh": false,
"extraInfo": null
"listA": [ "xyz", 24, null, true ],
"objectB": { "animal" : "lemur" }
}
],
"list" : [ "banana", 12, true, null, "orange", 12.5, { "ticker": "AAPL" }, [ 1, 2, 3, 4, 5 ] ],
"alien" : true
}
Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
set json [new_CkJsonObject]
# Load the JSON from a file.
set success [CkJsonObject_LoadFile $json "qa_data/json/modifySample.json"]
if {$success == 0} then {
puts [CkJsonObject_lastErrorText $json]
delete_CkJsonObject $json
exit
}
# This example will not check for errors (i.e. null / false / 0 return values)...
# Get the "list" array:
set listA [new_CkJsonArray]
CkJsonObject_ArrayOf2 $json "list" $listA
# Modify values in the list.
# Change banana to plantain
set success [CkJsonArray_SetStringAt $listA 0 "plantain"]
# Change 12 to 24
set success [CkJsonArray_SetIntAt $listA 1 24]
# Change true to false
set success [CkJsonArray_SetBoolAt $listA 2 0]
# Is the 3rd item null?
set bNull [CkJsonArray_IsNullAt $listA 3]
# Change "orange" to 32.
set success [CkJsonArray_SetIntAt $listA 4 32]
# Change 12.5 to 31.2
set success [CkJsonArray_SetNumberAt $listA 5 "31.2"]
# Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
# Do this by deleting, then inserting a new object at the same location.
set success [CkJsonArray_DeleteAt $listA 6]
set tickerObj [new_CkJsonObject]
CkJsonArray_AddObjectAt2 $listA 6 $tickerObj
set success [CkJsonObject_AppendString $tickerObj "ticker" "GOOG"]
# Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
set success [CkJsonArray_DeleteAt $listA 7]
set aa [new_CkJsonArray]
CkJsonArray_AddArrayAt2 $listA 7 $aa
set success [CkJsonArray_AddStringAt $aa -1 "apple"]
set success [CkJsonArray_AddIntAt $aa -1 22]
set success [CkJsonArray_AddBoolAt $aa -1 1]
set success [CkJsonArray_AddNullAt $aa -1]
set success [CkJsonArray_AddNumberAt $aa -1 "1080.25"]
# Get the "fruit" array
set aFruit [new_CkJsonArray]
CkJsonObject_ArrayAt2 $json 0 $aFruit
# Get the 1st element:
set appleObj [new_CkJsonObject]
CkJsonArray_ObjectAt2 $aFruit 0 $appleObj
# Modify values by member name:
set success [CkJsonObject_SetStringOf $appleObj "fruit" "fuji_apple"]
set success [CkJsonObject_SetIntOf $appleObj "count" 46]
set success [CkJsonObject_SetBoolOf $appleObj "fresh" 0]
set success [CkJsonObject_SetStringOf $appleObj "extraInfo" "developed by growers at the Tohoku Research Station in Fujisaki"]
# Modify values by index:
set pearObj [new_CkJsonObject]
CkJsonArray_ObjectAt2 $aFruit 1 $pearObj
set success [CkJsonObject_SetStringAt $pearObj 0 "bartlett_pear"]
set success [CkJsonObject_SetIntAt $pearObj 1 12]
set success [CkJsonObject_SetBoolAt $pearObj 2 0]
set success [CkJsonObject_SetStringAt $pearObj 3 "harvested in late August to early September"]
CkJsonObject_put_EmitCompact $json 0
puts [CkJsonObject_emit $json]
delete_CkJsonObject $json
delete_CkJsonArray $listA
delete_CkJsonObject $tickerObj
delete_CkJsonArray $aa
delete_CkJsonArray $aFruit
delete_CkJsonObject $appleObj
delete_CkJsonObject $pearObj