Have an amazing solution built in RAD Studio? Let us know. Looking for discounts? Visit our Special Offers page!
News

Use TRESTDataSetAdapter with arbitrary JSON

the future of starts demands massive productivity

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! ‘]

See What's New in 12.2 Athens See What's New in 12.2 Athens Dev Days of Summer 2-24

Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial   Upgrade Today

   Free Delphi Community Edition   Free C++Builder Community Edition

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

IN THE ARTICLES