Data Management

Update Keys in a List of Objects

Summary

When working with data from external APIs, you may encounter situations where the key names in the received objects don't match the field names in your database table. In such cases, you can't directly use the powerful `bulkAddRecords` function in Xano to add the data to your database. Instead, you would need to loop through the objects and add them individually, which can be less efficient.

Fortunately, Xano provides a convenient way to update the key names in a list of objects, allowing you to leverage the `bulkAddRecords` function and add multiple records to your database in a single step. In this guide, we'll walk you through the process step-by-step.

Step 1: Fetch the Data from the API

First, let's fetch the data from an external API. In this example, we'll use the free "Excuser API" which provides various excuses for getting out of situations.

javascript const api1 = await fetch.get('https://excuser-three.vercel.app/v1/excuse/30');

This API call will return a list of 30 excuses, where each excuse is represented as an object with the keys `id`, `excuse`, and `category`.

Step 2: Establish Variables

Next, we need to establish two variables: `values` and `keys`. The `values` variable will hold the actual values from the API response, and the `keys` variable will store the key names.

javascript const values = []; const keys = Object.keys(api1.response.result[0]);

The `keys` variable is initialized by accessing the first object in the API response (`api1.response.result[0]`) and extracting its keys using the `Object.keys()` method.

Step 3: Update the Key Names

In our example, we want to change the key `excuse` to `reason` to match the field name in our database table. We can achieve this by updating the `keys` array using its numerical index.

javascript keys[1] = 'reason';

Here, we're targeting the second item in the `keys` array (remember, arrays are zero-indexed) and updating its value to `'reason'`.

Step 4: Loop Through the Objects and Update the Keys

Now, we need to loop through all the objects in the API response and update their keys based on the modified `keys` array.

javascript for (const item of api1.response.result) { values.push( Object.fromEntries( Object.entries(item).map(([key, value]) => [keys[keys.indexOf(key)], value]) ) ); }

In this loop, we're iterating over each object in the `api1.response.result` array. For each object, we're creating a new object using `Object.fromEntries()` and mapping over the `Object.entries()` of the current object.

The `map()` function takes each key-value pair (`[key, value]`) and creates a new array with the updated key (from the `keys` array) and the original value. These new key-value pairs are then converted back into an object using `Object.fromEntries()`, which is pushed into the `values` array.

After this loop, the `values` array will contain all the objects from the API response, but with the key names updated according to the modified `keys` array.

Step 5: Add the Records to the Database

Finally, we can use the `bulkAddRecords` function in Xano to add all the updated objects to our database table in a single step.

javascript await datasource.bulkAddRecords('reasons', values);

In this example, we're adding the records to a table named `reasons` using the `values` array containing the updated objects.

And that's it! You've successfully updated the key names in a list of objects and added the data to your database table using the efficient `bulkAddRecords` function in Xano.

By following these steps, you can seamlessly work with data from external APIs, even when the key names don't match your database schema. Xano's powerful features allow you to manipulate and transform data quickly and efficiently, streamlining your development process and saving valuable time.

This transcript was AI generated to allow users to quickly answer technical questions about Xano.

Was this helpful?

I found it helpful

I need more support
Sign up for XanoSign up for Xano

Build without limits on a secure, scalable backend.

Unblock your team's progress and create a backend that will scale for free.

Start building for free