Data Management

Parsing through an object array and adding an object array to the database

Welcome to the exciting world of no-code application development with Xano! In this blog post, we'll guide you through the process of parsing object arrays and storing specific fields in your database using Xano's intuitive visual interface. Let's dive in!

Step 1: Understand Your Data Structure

Before we begin, let's take a look at the sample data we'll be working with:

var phoneNumbers = [
 { "type": "home", "number": "123-456-7890" },
 { "type": "work", "number": "987-654-3210" },
 { "type": "mobile", "number": "555-123-4567" }
]

In this example, we have an array called phoneNumbers containing three objects, each representing a phone number with a type and a number property.

Our goal is to extract just the number field from each object and store it in an array within our database.

Step 2: Flatten the Object Array

The first step is to flatten the nested object array structure. We'll do this by creating a new variable (var1) and parsing through the original phoneNumbers array:

var1 = phoneNumbers.flatMap(obj => Object.values(obj));

This will result in a new array var1 containing the individual objects without the "phone numbers" hierarchy:

var1 = [
 { "type": "home", "number": "123-456-7890" },
 { "type": "work", "number": "987-654-3210" },
 { "type": "mobile", "number": "555-123-4567" }
]

Step 3: Loop Through the Array and Extract the Numbers

Since we're dealing with an array, we need to loop through each item and extract the desired field. We'll use a forEach loop and create a new variable var2 to store the extracted numbers:

var2 = [];
var1.forEach(item => {
 var2.push(_.omit(item, 'type'));
});

In this code snippet, we're looping through var1 and pushing a new object to var2 that omits the type field, effectively leaving only the number property.

After this step, var2 will contain an array of objects with just the phone numbers:

var2 = [
 { "number": "123-456-7890" },
 { "number": "987-654-3210" },
 { "number": "555-123-4567" }
]

Step 4: Store the Data in Your Database

Now that we have the desired data structure, we can store it in our database. Assuming you have a user table with an info field that accepts an object array, you can simply update the record with var2:

await update('user', _.set('info', var2), {});

This will update the info field of the user table with the var2 array containing the phone numbers.

Step 5: Handle Different Field Names (Optional)

In some cases, the field names in your input data may differ from the ones in your database structure. For example, let's say your input data has the number field, but your database expects a phoneNumber field.

To handle this, you can modify the code in Step 3 as follows:

var2 = [];
var1.forEach(item => {
 var2.push({ phoneNumber: item.number });
});

Here, we're creating a new object with the desired field name (phoneNumber) and assigning the value from the number field of the input data.

After this modification, var2 will look like this:

var2 = [
 { "phoneNumber": "123-456-7890" },
 { "phoneNumber": "987-654-3210" },
 { "phoneNumber": "555-123-4567" }
]

Now, you can store this data in your database using the same command as in Step 4:

await update('user', _.set('info', var2), {});

And that's it! You've successfully parsed an object array, extracted specific fields, and stored them in your database using Xano's powerful visual interface.

Remember, this is just one example of what you can achieve with Xano. The platform offers a wide range of features and tools to simplify and streamline your application development process. Happy coding (or should we say, happy no-coding)!

Sign up for Xano

Join 100,000+ people already building with Xano.

Start today and scale to millions.

Start building for free