0

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.

  • Can you post the part of the swagger where you specify the type of the response? From the error message, it seems like the type is declared to be an object, but it is actually a string from the response you posted. – carlosfigueira Apr 27 '23 at 16:04
  • Yeah definetly: I am trying to read `object.Data.Data` that is a evaluated as a string, but Canvas is evaluating it as object and does not let me do any parsing. You can find swagger code here: https://gist.github.com/AlessandroMarc/1b9d709962de2c4590149dfee6432df5 – Alessandro Marchesin Apr 28 '23 at 07:12

1 Answers1

0

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:

enter image description here

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)

carlosfigueira
  • 85,035
  • 14
  • 131
  • 171
  • Agree with you! I've tried method two: I've defined Data as string in the Swagger Editor, but still when I try to Parse the output Cavas App still sees it as a table. Formula `ParseJSON(Connector.Action.Data.Data)` throws the error "Invalid argument type (Table). Expecting string". – Alessandro Marchesin May 02 '23 at 07:49