Importing Test Data
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:
- Copy the JSON object from the
GET /productsendpoint. - Head over to ChatGPT and ask it to convert the JSON object to a CSV.
- Copy the resulting CSV data and paste it into a text editor.
- Save the file with a
.csvextension (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:
- In the Xano dashboard, click on "Add Table" and select "Import from CSV".
- Choose the
products.csvfile you created earlier and click "Upload". - Xano will automatically detect the column types based on the data. However, if you have any columns that contain arrays (like the
imagescolumn), 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:
- In Xano, create a new API endpoint (e.g.,
import-dummy-data). - Inside the endpoint function, make a GET request to the
GET /productsendpoint on wjson.com. - Use a
forEachloop to iterate over theproductsarray in the response. - 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:
// 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:
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!
Sign up for Xano.
100,000+ people build on Xano.
Start today and scale to millions.