Sample code for 30+ languages & platforms
AutoIt

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 AutoIt Downloads

AutoIt
; Imagine we have two separate JSON objects.
$oJsonA = ObjCreate("Chilkat.JsonObject")
$oJsonA.UpdateString("animal","zebra")
$oJsonA.UpdateString("colors[0]","white")
$oJsonA.UpdateString("colors[1]","black")

$oJsonA.EmitCompact = False
ConsoleWrite($oJsonA.Emit() & @CRLF)

; jsonA contains:

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

$oJsonB = ObjCreate("Chilkat.JsonObject")
$oJsonB.UpdateString("type","mammal")
$oJsonB.UpdateBool("carnivore",False)

$oJsonB.EmitCompact = False
ConsoleWrite($oJsonB.Emit() & @CRLF)

; 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"
;   ]
; }

$oJsonA.AddObjectCopyAt(1,"info",$oJsonB)

ConsoleWrite($oJsonA.Emit() & @CRLF)

; The result is this:

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