Sample code for 30+ languages & platforms
Tcl

Insert JSON Object into another JSON Object

See more JSON Examples

Demonstrates how to insert one JSON object into another. Effectively, the JSON object must be copied into the other..

Chilkat Tcl Downloads

Tcl

load ./chilkat.dll

# Imagine we have two separate JSON objects.
set jsonA [new_CkJsonObject]

CkJsonObject_UpdateString $jsonA "animal" "zebra"
CkJsonObject_UpdateString $jsonA "colors[0]" "white"
CkJsonObject_UpdateString $jsonA "colors[1]" "black"

CkJsonObject_put_EmitCompact $jsonA 0
puts [CkJsonObject_emit $jsonA]

# jsonA contains:

# {
#   "animal": "zebra",
#   "colors": [
#     "white",
#     "black"
#   ]
# }

set jsonB [new_CkJsonObject]

CkJsonObject_UpdateString $jsonB "type" "mammal"
CkJsonObject_UpdateBool $jsonB "carnivore" 0

CkJsonObject_put_EmitCompact $jsonB 0
puts [CkJsonObject_emit $jsonB]

# jsonB contains:

# {
#   "type": "mammal",
#   "carnivore": false
# }

# Let's say we want to insert jsonB into jsonA to get this:

# {
#   "animal": "zebra",
#   "info" " {
#       "type": "mammal",
#       "carnivore": false
# 	},
#   "colors": [
#     "white",
#     "black"
#   ]
# }

CkJsonObject_AddObjectCopyAt $jsonA 1 "info" $jsonB

puts [CkJsonObject_emit $jsonA]

# The result is this:

# {
#   "animal": "zebra",
#   "info": {
#     "type": "mammal",
#     "carnivore": false
#   },
#   "colors": [
#     "white",
#     "black"
#   ]
# }

delete_CkJsonObject $jsonA
delete_CkJsonObject $jsonB