Skip to main content
Table of Contents

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 FileName property at design time — the file loads automatically.
  • Call LoadFromFile or LoadFromStream at runtime.
Loading a JSON FilePascalC++
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:

Loading a JSON FilePascalC++
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:

JSON
[
  { "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):

JSON
{
  "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:

Date and Time FormatPascalC++
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:

Configure, load, and persist structured JSONPascalC++
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, and DateTimeFormat before LoadFromFile; these values control how the input is interpreted.
  • DataRootElement must identify the array of records, while MetaDataElement identifies the separate field-description array. Swapping them prevents the dataset from receiving the expected fields and rows.
  • Enable AutoSaveChanges only when each dataset edit should immediately update FileName; otherwise save explicitly after a successful batch.
  • Keep FileEncoding aligned with the encoding passed to LoadFromFile. Automatic saving uses FileEncoding for 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

See Also