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

Create a JSON Array Containing an Object

See more JSON Examples

Creates a top-level JSON array containing an object.

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
  jArray: TJsonArray;
  json: TJsonObject;

begin
  jArray := TJsonArray.Create;

  json := TJsonObject.Create;
  jArray.AddObjectAt2(0,json);

  json.UpdateString('groupId','');
  json.UpdateString('sku','');
  json.UpdateString('title','');
  json.UpdateString('barcode','');
  json.UpdateString('category','');
  json.UpdateString('description','');
  json.UpdateString('images[0]','url1');
  json.UpdateString('images[1]','url...');
  json.UpdateString('isbn','');
  json.UpdateString('link','');
  json.UpdateString('linkLomadee','');
  json.UpdateString('prices[0].type','');
  json.UpdateNumber('prices[0].price','0');
  json.UpdateNumber('prices[0].priceLomadee','0');
  json.UpdateNumber('prices[0].priceCpa','0');
  json.UpdateNumber('prices[0].installment','0');
  json.UpdateNumber('prices[0].installmentValue','0');
  json.UpdateString('productAttributes."Atributo 1"','Valor 1');
  json.UpdateString('productAttributes."Atributo ..."','Valor ...');
  json.UpdateString('technicalSpecification."Especificação 1"','Valor');
  json.UpdateString('technicalSpecification."Especificação ..."','Valor ...');
  json.UpdateNumber('quantity','0');
  json.UpdateNumber('sizeHeight','0');
  json.UpdateNumber('sizeLength','0');
  json.UpdateNumber('sizeWidth','0');
  json.UpdateNumber('weightValue','0');
  json.UpdateNumber('declaredPrice','0');
  json.UpdateNumber('handlingTimeDays','0');
  json.UpdateBool('marketplace',False);
  json.UpdateString('marketplaceName','');

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

  //  The output of this program is:

  //  [
  //    {
  //      "groupId": "",
  //      "sku": "",
  //      "title": "",
  //      "barcode": "",
  //      "category": "",
  //      "description": "",
  //      "images": [
  //        "url1",
  //        "url..."
  //      ],
  //      "isbn": "",
  //      "link": "",
  //      "linkLomadee": "",
  //      "prices": [
  //        {
  //          "type": "",
  //          "price": 0,
  //          "priceLomadee": 0,
  //          "priceCpa": 0,
  //          "installment": 0,
  //          "installmentValue": 0
  //        }
  //      ],
  //      "productAttributes": {
  //        "Atributo 1": "Valor 1",
  //        "Atributo ...": "Valor ..."
  //      },
  //      "technicalSpecification": {
  //        "Especificação 1": "Valor",
  //        "Especificação ...": "Valor ..."
  //      },
  //      "quantity": 0,
  //      "sizeHeight": 0,
  //      "sizeLength": 0,
  //      "sizeWidth": 0,
  //      "weightValue": 0,
  //      "declaredPrice": 0,
  //      "handlingTimeDays": 0,
  //      "marketplace": false,
  //      "marketplaceName": ""
  //    }
  //  ]


  jArray.Free;
  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.