One basic question I get asked is how can I convert arbitrary JSON into some kind of data structure such that I can extract useful data?
One solution is to use the TRESTDataSetAdapter with arbitrary JSON.
The TRESTDataSetAdapter and TClientDataSet components convert the JSON representation into a dataset.
The TRESTDataSetAdapter has many nice functionalities, but some developers assume you are getting the JSON back from a REST call. But, you can use TRESTDataSetAdapter with arbitrary JSON.
Here's a sample Delphi application showing how to use the TRESTDataSetAdapter with arbitrary JSON:
Basically, this is how this application is implemented:
1. Clear last value: RESTResponseDataSetAdapter1.ResponseJSON := nil;
2. Root element is used to access a nested JSON array. For example set RootElement to 'x' to work with this JSON like this: { "x": [{“attr1”: “row1”}, {“attr1”: “row2”}]}
3. Parse the arbitrary JSON (from the memo in this example):
LJSON := TJSONObject.ParseJSONValue(Memo1.Lines.Text);
4. Lastly provide the JSON value to the adapter:
LIntf := TAdapterJSONValue.Create(LJSON);
RESTResponseDataSetAdapter1.ResponseJSON := LIntf;
RESTResponseDataSetAdapter1.Active := True;
5. Using Live Bindings, we bind the FireDAC MemTable (FDMemTable) to the StringGrid:
And this is how you can use the TRESTDataSetAdapter with arbitrary JSON.
The sample Delphi 10.2 Tokyo ResponseDataSetAdapter application can be downloaded from here.
RAD Studio includes many capabilities to work with JSON and REST.
[DownloadButton Product=’RAD’ Caption=’You can download a 30 day trial of RAD Studio and try it out for yourself! ‘]