Sample code for 30+ languages & platforms
AutoIt

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

AutoIt
Local $bSuccess = False

$oJson = ObjCreate("Chilkat.JsonObject")

; Load the JSON from a file.
$bSuccess = $oJson.LoadFile("qa_data/json/modifySample.json")
If ($bSuccess = False) Then
    ConsoleWrite($oJson.LastErrorText & @CRLF)
    Exit
EndIf

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

; Get the "list" array:

$oListA = ObjCreate("Chilkat.JsonArray")

$oJson.ArrayOf2("list",$oListA)

; Modify values in the list.

; Change banana to plantain
$bSuccess = $oListA.SetStringAt(0,"plantain")

; Change 12 to 24
$bSuccess = $oListA.SetIntAt(1,24)

; Change true to false
$bSuccess = $oListA.SetBoolAt(2,False)

; Is the 3rd item null?
Local $bNull = $oListA.IsNullAt(3)

; Change "orange" to 32.
$bSuccess = $oListA.SetIntAt(4,32)

; Change 12.5 to 31.2
$bSuccess = $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.
$bSuccess = $oListA.DeleteAt(6)
$oTickerObj = ObjCreate("Chilkat.JsonObject")
$oListA.AddObjectAt2(6,$oTickerObj)

$bSuccess = $oTickerObj.AppendString("ticker","GOOG")

; Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
$bSuccess = $oListA.DeleteAt(7)
$oAa = ObjCreate("Chilkat.JsonArray")
$oListA.AddArrayAt2(7,$oAa)

$bSuccess = $oAa.AddStringAt(-1,"apple")
$bSuccess = $oAa.AddIntAt(-1,22)
$bSuccess = $oAa.AddBoolAt(-1,True)
$bSuccess = $oAa.AddNullAt(-1)
$bSuccess = $oAa.AddNumberAt(-1,"1080.25")

; Get the "fruit" array
$oAFruit = ObjCreate("Chilkat.JsonArray")
$oJson.ArrayAt2(0,$oAFruit)

; Get the 1st element:
$oAppleObj = ObjCreate("Chilkat.JsonObject")
$oAFruit.ObjectAt2(0,$oAppleObj)

; Modify values by member name:
$bSuccess = $oAppleObj.SetStringOf("fruit","fuji_apple")
$bSuccess = $oAppleObj.SetIntOf("count",46)
$bSuccess = $oAppleObj.SetBoolOf("fresh",False)
$bSuccess = $oAppleObj.SetStringOf("extraInfo","developed by growers at the Tohoku Research Station in Fujisaki")

; Modify values by index:

$oPearObj = ObjCreate("Chilkat.JsonObject")
$oAFruit.ObjectAt2(1,$oPearObj)

$bSuccess = $oPearObj.SetStringAt(0,"bartlett_pear")
$bSuccess = $oPearObj.SetIntAt(1,12)
$bSuccess = $oPearObj.SetBoolAt(2,False)
$bSuccess = $oPearObj.SetStringAt(3,"harvested in late August to early September")

$oJson.EmitCompact = False
ConsoleWrite($oJson.Emit() & @CRLF)