Data Link — JSON
TTMSFNCDataLinkJSON loads JSON data into a TTMSFNCDataSet, enabling standard dataset operations (browse, filter, sort, bind) on JSON files.
Key class: TTMSFNCDataLinkJSON
Loading a JSON File
Drop TTMSFNCDataLinkJSON on the form and either:
- Set the
FileNameproperty at design time — the file loads automatically. - Call
LoadFromFileorLoadFromStreamat runtime.
TMSFNCDataLinkJSON1.FileName := 'data.json';
// or
TMSFNCDataLinkJSON1.LoadFromFile('data.json', TEncoding.UTF8);
TMSFNCDataLinkJSON1->FileName = "data.json";
// or
TMSFNCDataLinkJSON1->LoadFromFile("data.json", TEncoding::UTF8);
Link to a TTMSFNCDataSet:
TMSFNCDataSet1.DataLink := TMSFNCDataLinkJSON1;
TMSFNCDataSet1.Active := True;
TMSFNCDataSet1->DataLink = TMSFNCDataLinkJSON1;
TMSFNCDataSet1->Active = true;
JSON Structure
Simple Array (no DataRootElement)
When DataRootElement is empty, the JSON must be a flat array where each item has an identical structure:
[
{ "name": "John", "email": "john@example.com" },
{ "name": "Mark", "email": "mark@example.com" }
]
Structured JSON with Metadata
When MetaDataElement is set, the component reads field definitions from that element. Each field entry must have an "id" (JSON property name) and a "name" (display name in the dataset):
{
"columns": [
{ "id": "name", "name": "Name" },
{ "id": "email", "name": "Email" },
{ "id": "phoneNumber", "name": "Phone Number" }
],
"data": [
{ "name": "John", "email": "john@example.com", "phoneNumber": "(353) 01 222 3333" },
{ "name": "Mark", "email": "mark@gmail.com", "phoneNumber": "(01) 22 888 4444" }
]
}
Set DataRootElement := 'data' and MetaDataElement := 'columns'.
Date and Time Format
By default, ISO 8601 (yyyy-mm-ddThh:nn:ss.zzzZ) is used to detect and convert date values. Set DateTimeFormat to override with a custom format:
TMSFNCDataLinkJSON1.DateTimeFormat := 'yyyy/mm/dd';
TMSFNCDataLinkJSON1->DateTimeFormat = "yyyy/mm/dd";
Automatic Saving
Set AutoSaveChanges := True to persist modifications back to the file as they occur.
Combining Structure, Date Parsing, and Automatic Saving
For an object that contains metadata and a nested record array, set all structure and conversion options before loading. The same data link can then provide records to the dataset and persist later edits:
begin
TMSFNCDataSet1.Close;
TMSFNCDataSet1.DataLink := TMSFNCDataLinkJSON1;
TMSFNCDataLinkJSON1.DataRootElement := 'data';
TMSFNCDataLinkJSON1.MetaDataElement := 'columns';
TMSFNCDataLinkJSON1.DateTimeFormat := 'yyyy-mm-dd';
TMSFNCDataLinkJSON1.FileEncoding := feUTF8;
TMSFNCDataLinkJSON1.AutoSaveChanges := True;
TMSFNCDataLinkJSON1.LoadFromFile('customers.json', TEncoding.UTF8);
TMSFNCDataSet1.Open;
end;
TMSFNCDataSet1->Close();
TMSFNCDataSet1->DataLink = TMSFNCDataLinkJSON1;
TMSFNCDataLinkJSON1->DataRootElement = "data";
TMSFNCDataLinkJSON1->MetaDataElement = "columns";
TMSFNCDataLinkJSON1->DateTimeFormat = "yyyy-mm-dd";
TMSFNCDataLinkJSON1->FileEncoding = Tms::Tmsfnccustomdatalink::TTMSFNCCustomDataLinkFileEncoding::feUTF8;
TMSFNCDataLinkJSON1->AutoSaveChanges = true;
TMSFNCDataLinkJSON1->LoadFromFile("customers.json", TEncoding::UTF8);
TMSFNCDataSet1->Open();
Common Pitfalls
- Set
DataRootElement,MetaDataElement, andDateTimeFormatbeforeLoadFromFile; these values control how the input is interpreted. DataRootElementmust identify the array of records, whileMetaDataElementidentifies the separate field-description array. Swapping them prevents the dataset from receiving the expected fields and rows.- Enable
AutoSaveChangesonly when each dataset edit should immediately updateFileName; otherwise save explicitly after a successful batch. - Keep
FileEncodingaligned with the encoding passed toLoadFromFile. Automatic saving usesFileEncodingfor subsequent writes.
Methods
| Method | Description |
|---|---|
LoadFromFile(AFileName, AEncoding) |
Load from a file |
LoadFromStream(AStream, AEncoding) |
Load from a stream |
SaveToFile(AFileName, AEncoding) |
Save to a file |
SaveToStream(AStream, AEncoding) |
Save to a stream |
Properties
| Property | Description |
|---|---|
AutoSaveChanges |
Auto-persist changes (default: False) |
DataRootElement |
JSON key whose value is the data array |
DateTimeFormat |
Custom date/time format string (default: ISO 8601) |
FileEncoding |
Encoding for file operations |
FileName |
JSON file path |
MetaDataElement |
JSON key containing field definitions |