How to Build and Parse a JSON String with Objects

 
Download example on GitHub
 
The code below makes a JSON request, which includes objects and object properties. Also there is a code, which parses received JSON response and extracts all included object properties.
 
See also:
 
procedure TForm1.btnBuildJSONClick(Sender: TObject);
var
  request: TclJSONObject;
  data: TclJSONObject;
  job: TclJSONObject;
begin
  request := TclJSONObject.Create();
  try
    data := TclJSONObject.Create();
    request.AddMember('data', data);

    job := TclJSONObject.Create();
    data.AddMember('job', job);

    job.AddString('reference_id', 'ID32048');
    job.AddString('status_code', '10');
    job.AddString('status_name', 'New Job');

    ShowMessage(request.GetJSONString());
  finally
    request.Free();
  end;
end;
 
procedure TForm1.btnParseJSONClick(Sender: TObject);
var
  request: TclJSONObject;
  data: TclJSONObject;
  job: TclJSONObject;
begin
  request := TclJSONObject.ParseObject(Memo1.Lines.Text);
  try
    data := request.ObjectByName('data');

    job := data.ObjectByName('job');

    ShowMessage(job.ValueByName('reference_id'));
  finally
    request.Free();
  end;
end;
 

Add Feedback