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.
TTMSFNCDataLinkJSON1.FileName := 'data.json';
// or
TTMSFNCDataLinkJSON1.LoadFromFile('data.json', TEncoding.UTF8);
Link to a TTMSFNCDataSet:
TMSFNCDataSet1.DataLink := TTMSFNCDataLinkJSON1;
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:
TTMSFNCDataLinkJSON1.DateTimeFormat := 'yyyy/mm/dd';
Automatic Saving
Set AutoSaveChanges := True to persist modifications back to the file as they occur.
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 |