Sample code for 30+ languages & platforms
Lianja

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

Lianja
llSuccess = .F.

loJson = createobject("CkJsonObject")

// Load the JSON from a file.
llSuccess = loJson.LoadFile("qa_data/json/modifySample.json")
if (llSuccess = .F.) then
    ? loJson.LastErrorText
    release loJson
    return
endif

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

// Get the "list" array:

loListA = createobject("CkJsonArray")

loJson.ArrayOf2("list",loListA)

// Modify values in the list.

// Change banana to plantain
llSuccess = loListA.SetStringAt(0,"plantain")

// Change 12 to 24
llSuccess = loListA.SetIntAt(1,24)

// Change true to false
llSuccess = loListA.SetBoolAt(2,.F.)

// Is the 3rd item null?
llBNull = loListA.IsNullAt(3)

// Change "orange" to 32.
llSuccess = loListA.SetIntAt(4,32)

// Change 12.5 to 31.2
llSuccess = loListA.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.
llSuccess = loListA.DeleteAt(6)
loTickerObj = createobject("CkJsonObject")
loListA.AddObjectAt2(6,loTickerObj)

llSuccess = loTickerObj.AppendString("ticker","GOOG")

// Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
llSuccess = loListA.DeleteAt(7)
loAa = createobject("CkJsonArray")
loListA.AddArrayAt2(7,loAa)

llSuccess = loAa.AddStringAt(-1,"apple")
llSuccess = loAa.AddIntAt(-1,22)
llSuccess = loAa.AddBoolAt(-1,.T.)
llSuccess = loAa.AddNullAt(-1)
llSuccess = loAa.AddNumberAt(-1,"1080.25")

// Get the "fruit" array
loAFruit = createobject("CkJsonArray")
loJson.ArrayAt2(0,loAFruit)

// Get the 1st element:
loAppleObj = createobject("CkJsonObject")
loAFruit.ObjectAt2(0,loAppleObj)

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

// Modify values by index:

loPearObj = createobject("CkJsonObject")
loAFruit.ObjectAt2(1,loPearObj)

llSuccess = loPearObj.SetStringAt(0,"bartlett_pear")
llSuccess = loPearObj.SetIntAt(1,12)
llSuccess = loPearObj.SetBoolAt(2,.F.)
llSuccess = loPearObj.SetStringAt(3,"harvested in late August to early September")

loJson.EmitCompact = .F.
? loJson.Emit()


release loJson
release loListA
release loTickerObj
release loAa
release loAFruit
release loAppleObj
release loPearObj