Sample code for 30+ languages & platforms
Delphi ActiveX

Insert JSON Object into another JSON Object

See more JSON Examples

Demonstrates how to insert one JSON object into another. Effectively, the JSON object must be copied into the other..

Chilkat Delphi ActiveX Downloads

Delphi ActiveX
uses
    Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Chilkat_TLB;

...

procedure TForm1.Button1Click(Sender: TObject);
var
jsonA: TChilkatJsonObject;
jsonB: TChilkatJsonObject;

begin
// Imagine we have two separate JSON objects.
jsonA := TChilkatJsonObject.Create(Self);
jsonA.UpdateString('animal','zebra');
jsonA.UpdateString('colors[0]','white');
jsonA.UpdateString('colors[1]','black');

jsonA.EmitCompact := 0;
Memo1.Lines.Add(jsonA.Emit());

// jsonA contains:

// {
//   "animal": "zebra",
//   "colors": [
//     "white",
//     "black"
//   ]
// }

jsonB := TChilkatJsonObject.Create(Self);
jsonB.UpdateString('type','mammal');
jsonB.UpdateBool('carnivore',0);

jsonB.EmitCompact := 0;
Memo1.Lines.Add(jsonB.Emit());

// jsonB contains:

// {
//   "type": "mammal",
//   "carnivore": false
// }

// Let's say we want to insert jsonB into jsonA to get this:

// {
//   "animal": "zebra",
//   "info" " {
//       "type": "mammal",
//       "carnivore": false
// 	},
//   "colors": [
//     "white",
//     "black"
//   ]
// }

jsonA.AddObjectCopyAt(1,'info',jsonB.ControlInterface);

Memo1.Lines.Add(jsonA.Emit());

// The result is this:

// {
//   "animal": "zebra",
//   "info": {
//     "type": "mammal",
//     "carnivore": false
//   },
//   "colors": [
//     "white",
//     "black"
//   ]
// }
end;