Sample code for 30+ languages & platforms
AutoIt

Swap JSON Objects

See more JSON Examples

Demonstrates how to swap two JSON objects within a JSON document.

Chilkat AutoIt Downloads

AutoIt
Local $bSuccess = False

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

; Load the following JSON:

; {
;   "petter": {
;     "DOB": "26/02/1986",
;     "gender": "male",
;     "country": "US"
;   },
;   "Sara": {
;     "DOB": "13/05/1982",
;     "gender": "female",
;     "country": "FR"
;   },
;   "Jon": {
;     "DOB": "19/03/1984",
;     "gender": "male",
;     "country": "UK"
;   }
; }

$bSuccess = $oJson.LoadFile("qa_data/json/people.json")
If ($bSuccess = False) Then
    ConsoleWrite($oJson.LastErrorText & @CRLF)
    Exit
EndIf

; Swap the positions of Jon and Sara.
Local $index1 = $oJson.IndexOf("Jon")
Local $index2 = $oJson.IndexOf("Sara")
$oJson.Swap($index1,$index2)

; We have this now:
ConsoleWrite($oJson.Emit() & @CRLF)

; {
;   "petter": {
;     "DOB": "26/02/1986",
;     "gender": "male",
;     "country": "US"
;   },
;   "Jon": {
;     "DOB": "19/03/1984",
;     "gender": "male",
;     "country": "UK"
;   },
;   "Sara": {
;     "DOB": "13/05/1982",
;     "gender": "female",
;     "country": "FR"
;   }
; }

; To swap an inner member:

$oJsonSara = ObjCreate("Chilkat.JsonObject")
$oJson.ObjectOf2("Sara",$oJsonSara)

$index1 = $oJsonSara.IndexOf("DOB")
$index2 = $oJsonSara.IndexOf("country")
$oJsonSara.Swap($index1,$index2)

; We now have this:
ConsoleWrite($oJson.Emit() & @CRLF)

; {
;   "petter": {
;     "DOB": "26/02/1986",
;     "gender": "male",
;     "country": "US"
;   },
;   "Jon": {
;     "DOB": "19/03/1984",
;     "gender": "male",
;     "country": "UK"
;   },
;   "Sara": {
;     "country": "FR",
;     "gender": "female",
;     "DOB": "13/05/1982"
;   }
; }