Skip to main content
Importing data

Want to learn how you can import a CSV or a JSON file into your application,

Updated over 8 months ago

In this article, you'll learn how to:

  • Import files into your Betty Blocks application

Getting started

The use case for this example is to import a CSV file containing 'end-users'. The records include first name, last name, email, and an external identifier for each end-user. A link to a sample CSV file with end-users is provided below.

If you want to learn how to import other files and create a relation with another model based on a common field like the 'external identifier' used here, make sure to read our Indexed collections article.

Make sure to have a data model present with the following properties:

Properties of the end-user data model

With the data model ready let's get started.

Uploading your file

We'll begin by uploading the end-user data into the application. Download the CSV file provided below and upload it to your public files (accessible in the Tools section).

Copying the URL from the tickets.csv file that is uploaded in our public files

We'll use the link to the file later, the next step is to install the required action steps. We'll need these two action steps from the block store: 'Mass mutate' and 'Parse data to array'.

Creating the import functionality

After installing the steps, you can create a new action as follows:

Creating the action

Drag the Mass mutate step on the canvas and the Parse data to array step within it. The Mass mutate step doesn't need any configuration, the Parse data to array step needs a text variable with the link to the public file like this:

Adding the URL of the CSV file as a text variable

Press Save to save the variable inside the Parse data to array step, before we continue with the action we need to create a schema model. A schema model is used by the Parse data to array step to create a 'blueprint' to write the data towards, we can then use the blueprint in another action step like a Create record step later on.

Create a schema model called 'End-user Schema'. The properties within this model should exactly match the names of the columns in your source CSV file. In our case, add these properties:

Creating the schema model properties

With the schema model in place, let's navigate back to the Import end-users action and configure the Parse data to array step further with our new schema model:

Configuring the Parse data to array step

After configuring the step above drag a Loop step underneath it and configure it like this:

Configuring the loop step

To create individual records for each entry in the CSV file, we'll use a Loop step to iterate through the array from the Parse data to array step. Within the Loop, a Create record step will be added and configured as shown in the image below.

Configuring the Create record step

Notice that we can click the right arrow in the yielded section to select the properties of our 'End-user Schema' model. This allows us to map these selected properties to the corresponding properties in the model we created earlier.

Here's what the completed action will look like:

Overview of the action with all the action steps configured

In essence, we've used the Mass Mutate step to efficiently process data in batches. This avoids the inefficiency of looping and creating individual records, which would trigger a database query for each iteration.

The Parse data to array step facilitates this process by allowing us to define a schema model and iterate through the parsed data. Within the loop, a new record is created for each entry. Finally, the Mass mutate step efficiently processes the accumulated data once the loop completes.

Run the action

Run the action by hitting the Test run button in the top-right.

Sucessfully running the action


To verify the success of the action, create a new user management page using the back office template. Configure the template to display the properties we've added (first name, last name, email, and external identifier). Once created, navigate to the newly created page to confirm that the imported end-users, including their first names, last names, emails, and external identifiers, are displayed correctly.

Displaying the newly created record from the CSV file in a Data table view

This use case can be applied to other CSV or JSON files as well. You can use the knowledge from this use case in two ways:

  1. Recreate the action with adjustments: Build a new action with a different model, schema model, and public file URL specific to a different file that you want to import.

  2. Modify the existing action: Update the current action by changing the model, schema model, and public file URL specific to a different file that you want to import.

As stated at the beginning of this article, if you want to learn how to import other files and create a relation with another model based on a value like the external identifier in this example, make sure to read our Indexed collections article.

Did this answer your question?