Sample code for 30+ languages & platforms
PureBasic

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

PureBasic
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

    ; Imagine we have two separate JSON objects.
    jsonA.i = CkJsonObject::ckCreate()
    If jsonA.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(jsonA,"animal","zebra")
    CkJsonObject::ckUpdateString(jsonA,"colors[0]","white")
    CkJsonObject::ckUpdateString(jsonA,"colors[1]","black")

    CkJsonObject::setCkEmitCompact(jsonA, 0)
    Debug CkJsonObject::ckEmit(jsonA)

    ; jsonA contains:

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

    jsonB.i = CkJsonObject::ckCreate()
    If jsonB.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckUpdateString(jsonB,"type","mammal")
    CkJsonObject::ckUpdateBool(jsonB,"carnivore",0)

    CkJsonObject::setCkEmitCompact(jsonB, 0)
    Debug CkJsonObject::ckEmit(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::ckAddObjectCopyAt(jsonA,1,"info",jsonB)

    Debug CkJsonObject::ckEmit(jsonA)

    ; The result is this:

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


    CkJsonObject::ckDispose(jsonA)
    CkJsonObject::ckDispose(jsonB)


    ProcedureReturn
EndProcedure