TXDataJsonDataset from TDataset

IS there some way to convert from a TDataset to TXDataJsonDataset. It wan´t to copy the fields and data from a normal TDataSet to a TXDataJsonDataset so i can send it as response of the xData-Server.

What do you mean by a TDataset, exactly? TXDataJsonDataset inherits from TDataset, so technically speaking it's already a TDataset.

Baiscally what i want to do is pass on a Dataset with xData. I have built a xData-Server with a Service for testing.

With this Service i would like to fetch data from another server (which gets delivered as TFDMemTable) and pass it on to the client or Access it through a connection in the client.

Is there a way to achieve this?

TXDataJsonDataset works with JavaScript objects. Any JSON you receive from a XData service (or any service) that you parse into a JS object can be provided to the TXDataJsonDataset. The property names will be bound to dataset field names.

Thus, all your XData service should do is return a simple Delphi DTO class representing such object, or you can do it at a lower level and simply return a TJSONObject from your XData service.

Your XData service should return such DTO object (or JSON object) with its properties filled. I believe there are built-in mechanisms in TFDMemTable that does that job for you, or you can simply fill the objects manually.

What´s the required format for the TXDataJsonDataset.SetJsonData(Data: JSValue) to work?

I´ve replicated the structure from this API in the samples. But when i set the dataset to active, it tells me that no fields habe been found.

My Json looks like the following example with some other fields from the database. The $id-Field and the @xdata.type-Field are set to fixed values.

{
    "value": [
        {
            "$id": 1,
            "@xdata.type": "MemData",
            "FEILD1": "Test",
            "FEILD2": "XXX",
            "FIELD3": 3
        },
        {
            "$id": 2,
            "@xdata.type": "MemData",
            "FEILD1": "Test",
            "FEILD2": "XXX",
            "FIELD3": 3
        },
        {
            "$id": 3,
            "@xdata.type": "MemData",
            "FEILD1": "Test",
            "FEILD2": "XXX",
            "FIELD3": 3
        }
    ]
}

Any idea on how to solve that?

Just a JSON array of JSON objects. Each property in each JSON object can be read through a field with same name in the JSON dataset.

You should set the content of value property in the dataset.

OK, that has worked :)

But back to my first question. Can i somehow copy the Records from a Dataset (TFDMemTable) to into a TXDataJsonDataset.

I´ve managed to copy the Fielddefs like following:

var
    I: Integer;
    lJsonDataSet: TXDataJsonDataset;
begin
    lJsonDataSet := TXDataJsonDataset.Create(nil);
    lJsonDataSet.FieldDefs := FDMemTable1.FieldDefs;

    for I := 0 to lJsonDataSet .FieldDefs.Count - 1 do
    begin
      lJsonDataSet .FieldDefs[I].CreateField(lJsonDataSet );
    end;

    lJsonDataSet .Open;

    lJsonDataSet.Insert;           // Error at this line --> Data.DB >> TDataSet.Insert  >>  Buffer := ActiveBuffer;
    lJsonDataSet.CopyFields(Datenmodul.pgmsql);
    lJsonDataSet.Post;

end;

I´m kind of stuck with this now. Do you have an idea on how to fix that? I wan´t to send a list of TXDataJsonDataset as Result of a service.

With the normal TDataset this doesn´t work because i get a EJsonConverterNotFound-Error for TComponentname which is basically just a String:
image

Is it even possible to send a TXDataJsonDataset as Result?
A basic Dataset would suffice as well if we could solve the EJsonConverterNotFound-Error.

Best Regards and Thank you for the help so far
Max

Is anything here of interest?

I don't understand this:

What is TXDataJsonDataset? Is this code real? Because I'm not aware of any component named like that.

Even if you are referring to TXDataWebDataset, that's a component for TMS Web Core applications. And FireDac (and thus TFDMemTable) cannot be used in TMS Web Core applications, so it would also not make sense to mix both.

It´s defined in the XData.Web.JsonDataset:

My idea was to copy data from the TFDMemTable to a TXDataJsonDataset in the Service and then send the TXDataJsonDataset to the client.

TXDataJsonDataset is not available in the server. You should just return a normal JSON to it.
I believe FireDAC already as built-in converter to convert the dataset to a JSON format.

1 Like