Sample code for 30+ languages & platforms
Delphi DLL

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 Delphi DLL Downloads

Delphi DLL
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, JsonArray, JsonObject;

...

procedure TForm1.Button1Click(Sender: TObject);
var
success: Boolean;
json: HCkJsonObject;
listA: HCkJsonArray;
bNull: Boolean;
tickerObj: HCkJsonObject;
aa: HCkJsonArray;
aFruit: HCkJsonArray;
appleObj: HCkJsonObject;
pearObj: HCkJsonObject;

begin
success := False;

json := CkJsonObject_Create();

// Load the JSON from a file.
success := CkJsonObject_LoadFile(json,'qa_data/json/modifySample.json');
if (success = False) then
  begin
    Memo1.Lines.Add(CkJsonObject__lastErrorText(json));
    Exit;
  end;

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

// Get the "list" array:

listA := CkJsonArray_Create();

CkJsonObject_ArrayOf2(json,'list',listA);

// Modify values in the list.

// Change banana to plantain
success := CkJsonArray_SetStringAt(listA,0,'plantain');

// Change 12 to 24
success := CkJsonArray_SetIntAt(listA,1,24);

// Change true to false
success := CkJsonArray_SetBoolAt(listA,2,False);

// Is the 3rd item null?
bNull := CkJsonArray_IsNullAt(listA,3);

// Change "orange" to 32.
success := CkJsonArray_SetIntAt(listA,4,32);

// Change 12.5 to 31.2
success := CkJsonArray_SetNumberAt(listA,5,'31.2');

// Replace the { "ticker" : "AAPL" } object with { "ticker" : "GOOG" }
// Do this by deleting, then inserting a new object at the same location.
success := CkJsonArray_DeleteAt(listA,6);
tickerObj := CkJsonObject_Create();
CkJsonArray_AddObjectAt2(listA,6,tickerObj);

success := CkJsonObject_AppendString(tickerObj,'ticker','GOOG');

// Replace "[ 1, 2, 3, 4, 5 ]" with "[ "apple", 22, true, null, 1080.25 ]"
success := CkJsonArray_DeleteAt(listA,7);
aa := CkJsonArray_Create();
CkJsonArray_AddArrayAt2(listA,7,aa);

success := CkJsonArray_AddStringAt(aa,-1,'apple');
success := CkJsonArray_AddIntAt(aa,-1,22);
success := CkJsonArray_AddBoolAt(aa,-1,True);
success := CkJsonArray_AddNullAt(aa,-1);
success := CkJsonArray_AddNumberAt(aa,-1,'1080.25');

// Get the "fruit" array
aFruit := CkJsonArray_Create();
CkJsonObject_ArrayAt2(json,0,aFruit);

// Get the 1st element:
appleObj := CkJsonObject_Create();
CkJsonArray_ObjectAt2(aFruit,0,appleObj);

// Modify values by member name:
success := CkJsonObject_SetStringOf(appleObj,'fruit','fuji_apple');
success := CkJsonObject_SetIntOf(appleObj,'count',46);
success := CkJsonObject_SetBoolOf(appleObj,'fresh',False);
success := CkJsonObject_SetStringOf(appleObj,'extraInfo','developed by growers at the Tohoku Research Station in Fujisaki');

// Modify values by index:

pearObj := CkJsonObject_Create();
CkJsonArray_ObjectAt2(aFruit,1,pearObj);

success := CkJsonObject_SetStringAt(pearObj,0,'bartlett_pear');
success := CkJsonObject_SetIntAt(pearObj,1,12);
success := CkJsonObject_SetBoolAt(pearObj,2,False);
success := CkJsonObject_SetStringAt(pearObj,3,'harvested in late August to early September');

CkJsonObject_putEmitCompact(json,False);
Memo1.Lines.Add(CkJsonObject__emit(json));

CkJsonObject_Dispose(json);
CkJsonArray_Dispose(listA);
CkJsonObject_Dispose(tickerObj);
CkJsonArray_Dispose(aa);
CkJsonArray_Dispose(aFruit);
CkJsonObject_Dispose(appleObj);
CkJsonObject_Dispose(pearObj);

end;