Sample code for 30+ languages & platforms
PureBasic

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

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

Procedure ChilkatExample()

    success.i = 0

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

    ; Load the JSON from a file.
    success = CkJsonObject::ckLoadFile(json,"qa_data/json/modifySample.json")
    If success = 0
        Debug CkJsonObject::ckLastErrorText(json)
        CkJsonObject::ckDispose(json)
        ProcedureReturn
    EndIf

    ; This example will not check for errors (i.e. null / false / 0 return values)...

    ; Get the "list" array:

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

    CkJsonObject::ckArrayOf2(json,"list",listA)

    ; Modify values in the list.

    ; Change banana to plantain
    success = CkJsonArray::ckSetStringAt(listA,0,"plantain")

    ; Change 12 to 24
    success = CkJsonArray::ckSetIntAt(listA,1,24)

    ; Change true to false
    success = CkJsonArray::ckSetBoolAt(listA,2,0)

    ; Is the 3rd item null?
    bNull.i = CkJsonArray::ckIsNullAt(listA,3)

    ; Change "orange" to 32.
    success = CkJsonArray::ckSetIntAt(listA,4,32)

    ; Change 12.5 to 31.2
    success = CkJsonArray::ckSetNumberAt(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.
    success = CkJsonArray::ckDeleteAt(listA,6)
    tickerObj.i = CkJsonObject::ckCreate()
    If tickerObj.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckAddObjectAt2(listA,6,tickerObj)

    success = CkJsonObject::ckAppendString(tickerObj,"ticker","GOOG")

    ; Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
    success = CkJsonArray::ckDeleteAt(listA,7)
    aa.i = CkJsonArray::ckCreate()
    If aa.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckAddArrayAt2(listA,7,aa)

    success = CkJsonArray::ckAddStringAt(aa,-1,"apple")
    success = CkJsonArray::ckAddIntAt(aa,-1,22)
    success = CkJsonArray::ckAddBoolAt(aa,-1,1)
    success = CkJsonArray::ckAddNullAt(aa,-1)
    success = CkJsonArray::ckAddNumberAt(aa,-1,"1080.25")

    ; Get the "fruit" array
    aFruit.i = CkJsonArray::ckCreate()
    If aFruit.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonObject::ckArrayAt2(json,0,aFruit)

    ; Get the 1st element:
    appleObj.i = CkJsonObject::ckCreate()
    If appleObj.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkJsonArray::ckObjectAt2(aFruit,0,appleObj)

    ; Modify values by member name:
    success = CkJsonObject::ckSetStringOf(appleObj,"fruit","fuji_apple")
    success = CkJsonObject::ckSetIntOf(appleObj,"count",46)
    success = CkJsonObject::ckSetBoolOf(appleObj,"fresh",0)
    success = CkJsonObject::ckSetStringOf(appleObj,"extraInfo","developed by growers at the Tohoku Research Station in Fujisaki")

    ; Modify values by index:

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

    CkJsonArray::ckObjectAt2(aFruit,1,pearObj)

    success = CkJsonObject::ckSetStringAt(pearObj,0,"bartlett_pear")
    success = CkJsonObject::ckSetIntAt(pearObj,1,12)
    success = CkJsonObject::ckSetBoolAt(pearObj,2,0)
    success = CkJsonObject::ckSetStringAt(pearObj,3,"harvested in late August to early September")

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


    CkJsonObject::ckDispose(json)
    CkJsonArray::ckDispose(listA)
    CkJsonObject::ckDispose(tickerObj)
    CkJsonArray::ckDispose(aa)
    CkJsonArray::ckDispose(aFruit)
    CkJsonObject::ckDispose(appleObj)
    CkJsonObject::ckDispose(pearObj)


    ProcedureReturn
EndProcedure