The RandomUser API provides a collection of random user data in JSON format, ideal for developers building applications requiring random user profiles. The data includes personal details, location, and image URLs.
randomuser.json
- Basic user information, including
gender, name, location, and more.randomuser.json contains an array of user objects. Each object includes:
gender
: Gender of the user (e.g., 'female', 'male').name
: Contains title, first name, and last name of the user.location
: The user's location including street, city, state, and zip code.email
: The user's email address.username
: The user's unique username.password
: The hashed password of the user.phone
: The user's phone number.cell
: The user's cell number.picture
: URLs to the user's profile pictures in different sizes.Use JavaScript's fetch()
to retrieve random user data:
fetch('path/to/randomuser.json')
.then(response => response.json()) // Converts response into JSON
.then(data => {
console.log(data); // Logs the data for debugging
});
HTML structure to display a user:
<div id="user-profile"></div>
JavaScript to populate the user data:
document.addEventListener('DOMContentLoaded', () => {
const userProfile = document.getElementById('user-profile'); // Grabs the container element
// Fetch user data from JSON file
fetch('path/to/randomuser.json')
.then(response => response.json()) // Converts data into JSON format
.then(data => {
// Iterate through the user array and create individual user cards
data.forEach(user => {
const userCard = document.createElement('div'); // Creates a new div element for each user
// Populate the user card with relevant data using template literals
userCard.innerHTML = `
<h3>${user.name.first} ${user.name.last}</h3>
<p><strong>Gender:</strong> ${user.gender}</p>
<p><strong>Location:</strong> ${user.location.city}, ${user.location.state}</p>
<img src="${user.picture.large}" alt="${user.name.first}" width="150">
`;
// Append each card to the main profile container
userProfile.appendChild(userCard);
});
})
.catch(error => console.error('Error loading user data:', error)); // Handle errors gracefully
});
The RandomUser API is a great tool for generating random user data for applications. It helps developers easily integrate random profiles into their websites or applications, and can be especially useful for testing and mockups.