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

Copy JSON Object from one JSON Array to Another

See more JSON Examples

Demonstrates how to copy an object in a JSON array to another JSON array.

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.JsonArray,
  Chilkat.JsonObject;

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

procedure RunDemo;
var
  arr1: TJsonArray;
  arr2: TJsonArray;
  s: string;
  sEmpty: string;
  jObj: TJsonObject;

begin
  arr1 := TJsonArray.Create;
  arr2 := TJsonArray.Create;

  s := '[{"a":1}, {"b":2}, {"c":3}]';
  sEmpty := '[]';

  arr1.Load(s);
  arr2.Load(sEmpty);

  jObj := TJsonObject.Create;
  arr1.ObjectAt2(1,jObj);

  arr2.AddObjectCopyAt(-1,jObj);

  WriteLn(arr2.Emit());

  //  output is:   [{"b":2}]


  arr1.Free;
  arr2.Free;
  jObj.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.