Sample code for 30+ languages & platforms
PureBasic

Copy JSON Object from one JSON Array to Another

See more JSON Examples

Demonstrates how to copy an object in a JSON array to another JSON array.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkJsonArray.pb"
IncludeFile "CkJsonObject.pb"

Procedure ChilkatExample()

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

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

    s.s = "[{" + Chr(34) + "a" + Chr(34) + ":1}, {" + Chr(34) + "b" + Chr(34) + ":2}, {" + Chr(34) + "c" + Chr(34) + ":3}]"
    sEmpty.s = "[]"

    CkJsonArray::ckLoad(arr1,s)
    CkJsonArray::ckLoad(arr2,sEmpty)

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

    CkJsonArray::ckObjectAt2(arr1,1,jObj)

    CkJsonArray::ckAddObjectCopyAt(arr2,-1,jObj)

    Debug CkJsonArray::ckEmit(arr2)

    ; output is:   [{"b":2}]


    CkJsonArray::ckDispose(arr1)
    CkJsonArray::ckDispose(arr2)
    CkJsonObject::ckDispose(jObj)


    ProcedureReturn
EndProcedure