Taking in nested json object as a parameter (restful annotation)


#1

Edit: A solution has been found. You can wrap the data type in [] for an array or pass json as the data type for an object

With the restful annotation, you can pull data into a function from a JSON object as follows

@restful(endpoint='a')
type Example{

@restful(subPath='b')
fetch: function(a: string, b: string, c: string): json js server

}

Assuming your data looks something like:

{
"a": "value",
"b": "value",
"c": "value"
}

There are 3 strings in the object and 3 strings as the function’s parameters. This works fine because they are all primitive values.

What I’m not sure about is how to process that data if there are arrays or objects nested inside the json object, i.e.:

{
"a":  ["value1", "value2", "value3"],
"b": { "value": "value1"},
"c": "value"
}

It’s unclear to me how you’d set up your function to accept complex values as input. If you try to set up the input as a singular JSON object, you can’t then reference its fields by name like you’d expect. Any advice is much appreciated!