Sample code for 30+ languages & platforms
Pascal (Lazarus/Delphi)

JSON: Renaming and Deleting Members

See more JSON Examples

Demonstrates renaming and deleting members. This example uses the following JSON document:
{
   "apple": "red",
   "lime": "green",
   "banana": "yellow",
   "broccoli": "green",
   "strawberry": "red"
}

Chilkat Pascal (Lazarus/Delphi) Downloads

Pascal (Lazarus/Delphi)
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.JsonObject;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  json: TJsonObject;

begin
  success := False;

  json := TJsonObject.Create;

  success := json.Load('{"apple": "red","lime": "green","banana": "yellow","broccoli": "green","strawberry": "red"}');
  if (success <> True) then
    begin
      WriteLn(json.LastErrorText);
      Exit;
    end;

  //  Rename "lime" to "lemon".
  success := json.Rename('lime','lemon');
  //  Change the color to yellow:
  success := json.SetStringOf('lemon','yellow');

  //  Rename by index.  Banana is at index 2 (apple is at index 0)
  success := json.RenameAt(2,'bartlett_pear');

  //  Delete broccoli by name
  success := json.Delete('broccoli');

  //  Delete apple by index.  Apple is at index 0.
  success := json.DeleteAt(0);

  json.EmitCompact := False;
  WriteLn(json.Emit());


  json.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.