Import & export (JSON)
Load a JSON file, stream, or string directly into the grid. Map the JSON object structure to grid columns by supplying the key names that contain your row array and column values.
Overview
JSON import is built into the core grid. There is no separate JSON export (use CSV for round-trip data transfer); the JSON import is designed for loading data from REST APIs or configuration files.
| Operation | Method |
|---|---|
| Load from file | Grid.LoadFromJSONData(FileName) |
| Load from stream | Grid.LoadFromJSONStreamData(Stream) |
| Load from string | Grid.LoadFromJSONTextData(Text) |
All three share the same optional mapping parameters:
| Parameter | Type | Purpose |
|---|---|---|
ARowDataName |
string |
Key whose value is the JSON array of rows. Pass '' if the root is already an array. |
AColumnDataName |
string |
Key inside each row object whose value is the array of column values. Pass '' for flat row objects. |
AColumnNames |
TTMSFNCDataGridDataJSONNamesArray |
Maps JSON property names to specific column indices. |
Step by step
Flat array of objects
The most common shape — a JSON array at the root, each element an object with one property per column:
[
{ "id": 1, "name": "Alice", "country": "DE" },
{ "id": 2, "name": "Bob", "country": "US" }
]
Grid.ColumnCount := 3;
Grid.FixedRowCount := 1;
Grid.Cells[0, 0] := 'ID';
Grid.Cells[1, 0] := 'Name';
Grid.Cells[2, 0] := 'Country';
// Map JSON keys to columns in order
Grid.LoadFromJSONTextData(JsonText, '', '',
['id', 'name', 'country']); // AColumnNames: key → column order
AColumnNames is an array of property name strings; their position in the array corresponds to the grid column index.
Nested structure with a wrapper key
When the JSON has a wrapper object with a named array inside:
{
"data": [
{ "id": 1, "name": "Alice" },
{ "id": 2, "name": "Bob" }
]
}
Grid.LoadFromJSONData('customers.json', 'data', '',
['id', 'name']); // ARowDataName = 'data'
Array of arrays (columnar data)
When each row is a JSON array of values rather than an object:
{
"rows": [
{ "cols": [1, "Alice", "DE"] },
{ "cols": [2, "Bob", "US"] }
]
}
Grid.LoadFromJSONData('data.json',
'rows', // ARowDataName
'cols', // AColumnDataName — key inside each row object
nil); // no name mapping needed; columns are positional
Load from a stream (REST response)
var Response := TMemoryStream.Create;
try
HttpClient.Get('https://api.example.com/data', Response);
Response.Position := 0;
Grid.LoadFromJSONStreamData(Response, '', '', ['id', 'name', 'value']);
finally
Response.Free;
end;
Load from a string (TRestResponse)
Grid.LoadFromJSONTextData(RESTResponse1.Content, 'data', '',
['id', 'name', 'value']);
Transform values during import
OnLoadCellData fires for every cell as it is populated from the JSON, letting you convert or replace the parsed value before it is stored in the grid:
procedure TForm1.GridLoadCellData(Sender: TObject;
AColumn, ARow: Integer;
var AValue: TTMSFNCDataGridCellValue);
begin
// JSON may deliver numbers as strings; convert to TDateTime where needed
if AColumn = DateColumn then
begin
var D: TDateTime;
if TryStrToDateTime(AValue.AsString, D) then
AValue := TValue.From<TDateTime>(D);
end;
end;
Related API
Grid.LoadFromJSONData(AFileName, ARowDataName, AColumnDataName, AColumnNames)Grid.LoadFromJSONStreamData(AStream, ARowDataName, AColumnDataName, AColumnNames)Grid.LoadFromJSONTextData(AText, ARowDataName, AColumnDataName, AColumnNames)TTMSFNCDataGridDataJSONNamesArray—TArray<string>of JSON key names mapped to column indicesOnLoadCellData— transform a cell value as it is read from the JSON source.
See also
- Import & export (CSV) — CSV is better for round-trip data transfer.
- Import & export (XLS) — Excel import/export with FlexCel.
- Data binding — for live dataset connections.