Response Data
Working with JSON, inbound and outbound
One of the tasks we can perform with a Summit model is data transformation: taking data from one place and changing it before sending it elsewhere.
Response
SEL offers an event type called Response
which allows us to compose a response object made of JSON.
Not the entire response.
Despite its name, this event does not represent the entire response of your model. For that, you should look at the models
Endpoints
which are listed on the endpoints screen. Instead,Response
inside of a model controls only what will appear in thedata
field of your endpoints.
We can use a Response
event type by instantiating it with an attribute using text:
=Response("items[]")
Or
=Response("answer")
In the former case, applying the []
tells SEL that this is an array. Any data sent to this event will be appended to the items
array of the data
attribute of your model's API response, like so:
// THE REST OF YOUR MODELS RESPONSE
{
"data":
[
{
"items": ["mary", "lamb", "little"]
}
]
}
Whereas the use of answer
will compose like so:
// THE REST OF YOUR MODELS RESPONSE
{
"data":
[
{
"answer": "little"
}
]
}
Notice "little"
appears as the value. That's because, assuming we sent these values in order, each subsequent value overwrote what was stored in the data
object at this attribute.
The text passed into the Response
event is a path. This means we can specify deeper locations to place data:
=Response("books.young_adult_fiction.items[]")
Will allow us to append to an items
attribute nested beneath a higher-level young_adult_fiction
object which is nested inside of a books
object. Any entries in the path that do not exist at the time of assignment will be created on-the-fly.
The Response
event is a singleton: there is only one. This means you can compose complex JSON structures by using multiple Response
events with different paths or locations of where to append data. For example, if your canvas has these two events:
=Response("foo")
and
=Response("bar[]")
Your output would look like
// THE REST OF YOUR MODELS RESPONSE
{
"data":
[
{
"foo": "dr pepper",
"bar": ["diet pepsi", "coca-cola", "sprite"]
}
]
}
Data is an array
As you can see in the above blocks, the
data
entry of your overall model API response is a list (array) with a single entry (yourResponse
data). This is by design, as many platforms, especially in the no-code space, prefer to receive and process lists of data instead of unfamiliar objects (yourResponse
) at the top level.
Updated 18 days ago