Sample code for 30+ languages & platforms
AutoIt

JSON: Miscellaneous Operations

See more JSON Examples

Demonstrates a variety of JSON API methods. This example uses the following JSON document:
{
   "alphabet": "abcdefghijklmnopqrstuvwxyz",
   "sampleData" : {
           "pi": 3.14,
	   "apple": "juicy",
	   "hungry": true,
	   "withoutValue": null,
           "answer": 42
          
	}
}

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

$oJson = ObjCreate("Chilkat.JsonObject")
$oJson.EmitCompact = False

; Assume the file contains the data as shown above..
$bSuccess = $oJson.LoadFile("qa_data/json/sample2.json")
If ($bSuccess = False) Then
    ConsoleWrite($oJson.LastErrorText & @CRLF)
    Exit
EndIf

; Get the "sampleData" object:
$oSampleData = ObjCreate("Chilkat.JsonObject")
$oJson.ObjectOf2("sampleData",$oSampleData)

; Demonstrate BoolAt and BoolOf
ConsoleWrite("hungry: " & $oSampleData.BoolOf("hungry") & @CRLF)
ConsoleWrite("hungry: " & $oSampleData.BoolAt(2) & @CRLF)

; StringOf returns the value as a string regardless of it's actual type:
ConsoleWrite("pi: " & $oSampleData.StringOf("pi") & @CRLF)
ConsoleWrite("answer: " & $oSampleData.StringOf("answer") & @CRLF)
ConsoleWrite("withoutValue: " & $oSampleData.StringOf("withoutValue") & @CRLF)
ConsoleWrite("hungry: " & $oSampleData.StringOf("hungry") & @CRLF)

; Demonstrate IsNullOf / IsNullAt
ConsoleWrite("withoutValue is null? " & $oSampleData.IsNullOf("withoutValue") & @CRLF)
ConsoleWrite("withoutValue is null? " & $oSampleData.IsNullAt(3) & @CRLF)
ConsoleWrite("apple is null? " & $oSampleData.IsNullOf("apple") & @CRLF)
ConsoleWrite("apple is null? " & $oSampleData.IsNullAt(1) & @CRLF)

; IntOf
ConsoleWrite("answer: " & $oSampleData.IntOf("answer") & @CRLF)

; SetNullAt, SetNullOf
; Set "pi" to null
$bSuccess = $oSampleData.SetNullAt(0)
; Set "answer" to null
$bSuccess = $oSampleData.SetNullOf("answer")

; Show the changes:
ConsoleWrite($oJson.Emit() & @CRLF)

; Restore pi and apple:
$bSuccess = $oSampleData.SetNumberAt(0,"3.14")
$bSuccess = $oSampleData.SetNumberOf("answer","42")

; Show the changes:
ConsoleWrite($oJson.Emit() & @CRLF)

; Add a null value named "afterApple" just after "apple"
$bSuccess = $oSampleData.AddNullAt(2,"afterApple")

; Add a boolean value just after "pi"
$bSuccess = $oSampleData.AddBoolAt(1,"afterPi",False)

; Examine the changes..
ConsoleWrite($oJson.Emit() & @CRLF)