File Management

Importing Test Data

Summary

Hey there! In this guide, we'll walk through the process of importing test data into your Xano database using the wjson.com API. This can be useful when you're starting a new project and need some sample data to work with, or if you want to see how your frontend will display data from the backend.

Step 1: Exploring wjson.com Endpoints

First, let's take a look at the endpoints available on wjson.com. Head over to the wjson.com Docs and you'll find a list of endpoints on the left-hand side. These include products, carts, users, posts, comments, and more. While these endpoints are designed for an e-commerce site, you can use them to generate test data for any kind of project.

For this guide, we'll focus on the `GET /products` endpoint, which retrieves all product data.

Step 2: Converting the Data to CSV

To import the data into Xano, we'll first need to convert it to a CSV file. Here's how:

  1. Copy the JSON object from the `GET /products` endpoint.
  2. Head over to ChatGPT and ask it to convert the JSON object to a CSV.
  3. Copy the resulting CSV data and paste it into a text editor.
  4. Save the file with a `.csv` extension (e.g., `products.csv`).

Step 3: Creating a Table in Xano

Now that we have our CSV file, let's create a table in Xano to store the data:

  1. In the Xano dashboard, click on "Add Table" and select "Import from CSV".
  2. Choose the `products.csv` file you created earlier and click "Upload".
  3. Xano will automatically detect the column types based on the data. However, if you have any columns that contain arrays (like the `images` column), you'll need to manually set the column type to "List".

Step 4: Importing the Data

With our table set up, we can now import the data from wjson.com. Here's how:

  1. In Xano, create a new API endpoint (e.g., `import-dummy-data`).
  2. Inside the endpoint function, make a GET request to the `GET /products` endpoint on wjson.com.
  3. Use a `forEach` loop to iterate over the `products` array in the response.
  4. For each product, create a new record in your Xano table, mapping the fields from the API response to the corresponding columns in your table.

Here's an example of how the code might look:

javascript // Make the GET request to wjson.com const response = await fetch('https://wjson.com/products'); const data = await response.json(); // Loop through the products and create records data.products.forEach(product => { createRecord('products', { id: product.id, title: product.title, price: product.price, // ... and so on for each column }); });

Step 5: Handling Pagination

The wjson.com API uses `limit` and `skip` parameters for pagination. By default, it returns 30 results per request, but you can adjust these parameters to retrieve more data.

To import all available data, we'll need to make multiple requests, incrementing the `skip` parameter each time until we've retrieved all records.

Here's how you can modify the code from Step 4 to handle pagination:

javascript let skip = 0; const limit = 30; const totalProducts = data.total; while (skip < totalProducts) { const response = await fetch(`https://wjson.com/products?skip=${skip}&limit=${limit}`); const data = await response.json(); data.products.forEach(product => { createRecord('products', { id: product.id, title: product.title, price: product.price, // ... and so on for each column }); }); skip += limit; }

This code uses a `while` loop to continuously make requests to the API, incrementing the `skip` parameter by `limit` each time, until all records have been retrieved and imported into your Xano table.

And that's it! You've successfully imported test data from wjson.com into your Xano database. You can now use this data to test your application's functionality or showcase it to potential users or investors.

If you have any questions or need further assistance, feel free to reach out to the Xano support team or join the community forums. Happy coding!

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