How to fetch nested objects from JSON responses

Sam van de Kreeke

Published: 25-10-2019

Last updated: 27-08-2020

Setting up the Webservice Endpoint

Create a new Endpoint

Opening the Webservices page will give you an overview of all the Webservices present in your application. Here you’ll have to select the Example Webservice that was created for HowTo fetch one object from JSON responses?

In order to create an Endpoint, you’ll have to follow the first few steps of the HowTo fetch one object from JSON responses? But instead of using the comments path use the users path. This’ll look something like the following:

The structure of users is as follows:

Again you should run the test and generate a Custom model, except it’ll be a little different this time. When you open the attributes of your custom model you’ll notice that there are multiple attributes that have a Custom model Kind. 

These attributes are Custom models themselves, meaning that they contain multiple attributes.

Create a new Action

Opening the Actions page will give you an overview of all the Actions present in your application. Click on the New button located in the top left corner. Fill in the description and specify which model the Action is for. Again keep the trigger on Manual. Your form should look something like this:

Create an Http request

Select the Http request event option in the Kind dropdown. Select the right Webservice and Webservice endpoint. Insert the path variable from the variable browser. In this case, just use a static number(1) but you can also make it dynamic by using a variable. Last but not least you have to give the response a variable name for further processing. Your form should look something like this:

After you’ve created the Http request Action event you’ll have to create a new Action event. Create a Create Action event in order to create the record and save it into your Back Office. Select the Model that you want the record to be saved to, now you’ll have to assign every Comment property to the attributes of our Custom model. However, in comparison with HowTo fetch one object from JSON responses? You’ll have to create multiple Create Action events. The reason for this is that you have to create a Create Action event for every nested object. 

You need to start with the deepest layer of your response, as it is needed in order to create the relations between the models. In order to create these relations, you will need to specify them in the Create Action event of the layer above. This should look something like this:

After you have done this for all the layers of the response you should end up with a Action that should look something like this:

Run Action

In order to run the Action, you’ll need to select the Action and click the Run button located in the top right corner of Betty Blocks. A popup will show, you’ll just have to click the execute button located in the bottom right of the popup. A success message should appear stating the custom model has been created.

View Back Office

In order to see the created records, you’ll have to direct yourself to the Grid page of the specific model in your Back Office. If the Action was successfully run then your Grid should look something like this:

In this tutorial

Not sure where to start?

Click here to register and start learning