0

I am calling the "Actions" endpoint on the ActionStep API and it's not returning the expecting JSON results.

When i call the API it can return one or many "Actions", a List.

public List<Action> actions { get; set; }

Which is what the model I am trying to deserialize into reflects. In my mind, this should work even if the list only contains one single Action.

But, if there is 1, 51, 101, 151, etc, Actions in the system the JSON is not coming back as a "LIST/ARRAY" it comes back as a single Action. (The endpoint returns 50 results per page).

Here is an example of what is returned on page 20 of the call to the api:

(There are 50 results per page and 951 actions in the system)

{"links":{"actions.assignedTo":{"href":"https:\/\/us-east-1.actionstep.com.actionstep.com\/api\/rest\/participants\/{actions.assignedTo}","type":"participants"},"actions.actionType":{"href":"https:\/\/us-east-1.actionstep.com.actionstep.com\/api\/rest\/actiontypes\/{actions.actionType}","type":"actiontypes"},"actions.primaryParticipants":{"href":"https:\/\/us-east-1.actionstep.com.actionstep.com\/api\/rest\/participants\/{actions.primaryParticipants}","type":"participants"},"actions.relatedActions":{"href":"https:\/\/us-east-1.actionstep.com.actionstep.com\/api\/rest\/actions\/{actions.relatedActions}","type":"actions"}},"actions":{"id":3952,"name":"Corporate","reference":null,"priority":0,"status":"Active","statusTimestamp":"2019-10-10T13:52:51-05:00","isBillableOverride":null,"createdTimestamp":"2019-10-10","modifiedTimestamp":"2019-10-10T13:56:33-05:00","isDeleted":"F","deletedBy":null,"deletedTimestamp":null,"isFavorite":"F","overrideBillingStatus":null,"lastAccessTimestamp":"2019-11-15T10:30:54-06:00","links":{"assignedTo":"9","actionType":"6","primaryParticipants":["4"],"relatedActions":null}},"meta":{"paging":{"actions":{"recordCount":951,"pageCount":20,"page":20,"pageSize":50,"prevPage":"https:\/\/us-east-1.actionstep.com\/api\/rest\/actions?page=19","nextPage":null}},"debug":{"requestTime":"0.084719","mem":"2.43mb","server":"app-ip-10-11-35-182.us-east-1.actionstep.com","cb":"T21726-22107-2","time":"2019-11-20 00:16:17 -0600 (America\/Chicago)","appload":"0.01, 0.04, 0.05","app":"0.017644","db":"0.067075","dbc":"0.008073","qc":"27","uqc":"23","fc":"751","rl":null}}}

On every page prior to page 20 the JSON for the "Actions" comes back as an array. On page 20, as it only contains 1 Action it swaps to a single item.

When i deserialize pages 1 to 19, my code works perfectly. But when it tries page 20 it fails with:

Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`

I am struggling to come up with a way to handle this.

My idea is to somehow check the raw JSON before deserializing it to see if there is an array of Actions or a single Action. If there is a single Action somehow then "hack" the JSON string to make it an array.

This would be somekind of string search/replace.

I can see how to do the search/replace to change

"actions":{

to

"actions":[{

But I cannot work out how to search/replace the end of the Action JSON and close the array.

Is this the best strategy for solving this?

Can anyone suggest how to change it to an array?

UPDATE:

I have a suggested fix in the first 2 comments below but I am strugling to get it working. I believe because my JSON is more complicated than the example.

I have uploaded a working page and a broken below:

Working: https://codebeautify.org/jsonviewer/cb25f016

Broken: https://codebeautify.org/jsonviewer/cbd79451

Your help is very much appreciated.

Trevor Daniel
  • 3,785
  • 12
  • 53
  • 89
  • You don't have to modify the Json it self just use the linked question. add a custom converter that will handle both and return either an array with one item or the normal array. – xdtTransform Nov 20 '19 at 07:37
  • @xdtTransform Thanks for the help. That does look very similar. It's just I am struggling to get it working in my code. – Trevor Daniel Nov 20 '19 at 07:57
  • @xdtTransform Yes, the first is the working page where "Actions" are an array. The second page is where it's coming back as a single item. – Trevor Daniel Nov 20 '19 at 08:04
  • 1
    @xdtTransform Apologies! I have it working! – Trevor Daniel Nov 20 '19 at 08:09

0 Answers0