Sample code for 30+ languages & platforms
Xbase++ Requires Chilkat v11.0.0+

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 Xbase++ Downloads

Xbase++
LOCAL nSuccess
LOCAL oJson
LOCAL oListA
LOCAL nBNull
LOCAL oTickerObj
LOCAL oAa
LOCAL oAFruit
LOCAL oAppleObj
LOCAL oPearObj

nSuccess := 0

oJson := CreateObject("Chilkat.JsonObject")

//  Load the JSON from a file.
nSuccess := oJson:LoadFile("qa_data/json/modifySample.json")
IF (nSuccess == 0)
    ? oJson:LastErrorText
    oJson:destroy()
    RETURN
ENDIF

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

//  Get the "list" array:

oListA := CreateObject("Chilkat.JsonArray")

oJson:ArrayOf2("list", oListA)

//  Modify values in the list.

//  Change banana to plantain
nSuccess := oListA:SetStringAt(0, "plantain")

//  Change 12 to 24
nSuccess := oListA:SetIntAt(1, 24)

//  Change true to false
nSuccess := oListA:SetBoolAt(2, 0)

//  Is the 3rd item null?
nBNull := oListA:IsNullAt(3)

//  Change "orange" to 32.
nSuccess := oListA:SetIntAt(4, 32)

//  Change 12.5 to 31.2
nSuccess := oListA:SetNumberAt(5, "31.2")

//  Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
//  Do this by deleting, then inserting a new object at the same location.
nSuccess := oListA:DeleteAt(6)
oTickerObj := CreateObject("Chilkat.JsonObject")
oListA:AddObjectAt2(6, oTickerObj)

nSuccess := oTickerObj:AppendString("ticker", "GOOG")

//  Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
nSuccess := oListA:DeleteAt(7)
oAa := CreateObject("Chilkat.JsonArray")
oListA:AddArrayAt2(7, oAa)

nSuccess := oAa:AddStringAt(-1, "apple")
nSuccess := oAa:AddIntAt(-1, 22)
nSuccess := oAa:AddBoolAt(-1, 1)
nSuccess := oAa:AddNullAt(-1)
nSuccess := oAa:AddNumberAt(-1, "1080.25")

//  Get the "fruit" array
oAFruit := CreateObject("Chilkat.JsonArray")
oJson:ArrayAt2(0, oAFruit)

//  Get the 1st element:
oAppleObj := CreateObject("Chilkat.JsonObject")
oAFruit:ObjectAt2(0, oAppleObj)

//  Modify values by member name:
nSuccess := oAppleObj:SetStringOf("fruit", "fuji_apple")
nSuccess := oAppleObj:SetIntOf("count", 46)
nSuccess := oAppleObj:SetBoolOf("fresh", 0)
nSuccess := oAppleObj:SetStringOf("extraInfo", "developed by growers at the Tohoku Research Station in Fujisaki")

//  Modify values by index:

oPearObj := CreateObject("Chilkat.JsonObject")
oAFruit:ObjectAt2(1, oPearObj)

nSuccess := oPearObj:SetStringAt(0, "bartlett_pear")
nSuccess := oPearObj:SetIntAt(1, 12)
nSuccess := oPearObj:SetBoolAt(2, 0)
nSuccess := oPearObj:SetStringAt(3, "harvested in late August to early September")

oJson:EmitCompact := 0
? oJson:Emit()

oJson:destroy()
oListA:destroy()
oTickerObj:destroy()
oAa:destroy()
oAFruit:destroy()
oAppleObj:destroy()
oPearObj:destroy()