I have created a custom connector to make a post request to our REST Service that returns an array of objects nested in another object. My goal is to work with object.Data.Data.
{
"User": "55205bb2-28c4-ed11-83ff-0022489ede2f",
"ErrorMessage": null,
"Data": "{\"TotalRecordCount\":1,\"TotalRecordExcluded\":0,\"Data\":[{\"Id\":\"13a0293d-c7de-ed11-a7c6-0022489ede2f\",\"Name\":\"Second trial!\",\"FileName\":\"Another File Name\",\"TotalRows\":14,\"IsProcessed\":false,\"Date\":\"2023-04-17T22:00:00Z\",\"ExpectedFileDate\":\"2023-04-03T22:00:00Z\",\"IsCompleted\":false,\"ErrorMessages\":null,\"Excluded\":false,\"LogicalName\":\"eps_datacollectionfile\"}]}"
}
This is the method in the service called:
try
{
Log.Debug($@"X-Correlation-ID {CorrelationId} - START: GetMultipleFileInfo- ids {ids}");
List<MsdDataCollectionFile> fileinfo = GetMultipleFileInfoData(ids);
dynamic paged = new ExpandoObject();
paged.TotalRecordCount = fileinfo.Count;
paged.TotalRecordExcluded = fileinfo.Where(x => x.Excluded).Count();
paged.Data = fileinfo;
Log.Debug($@"X-Correlation-ID {CorrelationId} - END: GetMultipleFileInfoData- totalrecord {fileinfo.Count}");
return new HttpResponse(((System.Dynamic.ExpandoObject)paged).ToJSON(), null);
}
I call it in a custom app creating a collection passing the parameters it expects (with
ClearCollect(connectorCollection, DataCollection.GetFile(userId, "application/json", {body:"13a0293d-c7de-ed11-a7c6-0022489ede2f"}).Data.Data))
It seems ok because the gallery in Canva gets correctly the data model. image: Canvas editor
However, when launched from the model-driven app, it doesn't work, saying, "JSON parsing error, expected 'object' but got 'string'." image: DevTools
Does anyone of you have any idea on what I can fix in the parsing?
I was trying to fetch an object from a Canvas App through a Custom Connector, and expecting to be able to read the object.
The type of the field Data
in your response is a string (a JSON-encoded string, but a string nonetheless), and your swagger is expecting it to be an object:
You have two options - either fix the service to return it as an object as expected by the swagger, or change the swagger to declare that the 'Data' property is a string, and use the ParseJSON function to work with the data (see https://powerapps.microsoft.com/en-us/blog/power-fx-introducing-parsejson/ and https://learn.microsoft.com/en-us/power-platform/power-fx/reference/function-parsejson for more details)