Tools 7 min read March 10, 2026

How to Generate Realistic Mock JSON Data for Testing

A developer's guide to generating realistic mock JSON data using Faker.js, JSON Schema generators, and JSON Operations' built-in generator for test datasets.

Using real production data in development and testing environments is a security risk and a GDPR liability. Good mock data that accurately reflects the shape and variety of real data is the solution. Generating it efficiently is a core developer skill.

Why Mock Data Matters

  • Security: Real user emails, names, and payment data must never appear in test databases, CI logs, or developer laptops.
  • Privacy compliance: GDPR, CCPA, and HIPAA restrict how PII can be used. Mock data sidesteps these regulations entirely.
  • Repeatability: Tests that depend on real data break when that data changes. Mock data is deterministic and version-controlled.
  • Scale testing: You can generate 1 million mock users in seconds. Getting 1 million real users into a test database is impractical.
  • Edge case coverage: Mock data generators can be configured to produce edge cases (empty strings, null values, maximum-length inputs) that might not exist in production datasets.

Getting Started with Faker.js

Faker.js (`@faker-js/faker`) is the most widely used JavaScript library for generating realistic fake data. It provides hundreds of data providers covering personal information, addresses, internet data, financial data, dates, and more.

bash
npm install @faker-js/faker
javascript
import { faker } from '@faker-js/faker';

// Generate a single realistic user
function generateUser() {
  const firstName = faker.person.firstName();
  const lastName = faker.person.lastName();
  return {
    id: faker.string.uuid(),
    firstName,
    lastName,
    email: faker.internet.email({ firstName, lastName }),
    username: faker.internet.username({ firstName, lastName }),
    avatarUrl: faker.image.avatar(),
    phone: faker.phone.number(),
    dateOfBirth: faker.date.birthdate({ min: 18, max: 80, mode: 'age' }),
    address: {
      street: faker.location.streetAddress(),
      city: faker.location.city(),
      state: faker.location.state(),
      zipCode: faker.location.zipCode(),
      country: faker.location.country(),
    },
    createdAt: faker.date.past({ years: 3 }),
  };
}

// Generate 100 users
const users = Array.from({ length: 100 }, generateUser);
console.log(JSON.stringify(users, null, 2));

Seeding for Reproducibility

By default, Faker generates different data on every run. For snapshot tests and reproducible CI builds, use a fixed seed:

javascript
import { faker } from '@faker-js/faker';

// Same seed = same output every time
faker.seed(12345);

const user = {
  name: faker.person.fullName(), // "Mrs. Jane Smith" (same value every run)
  email: faker.internet.email(), // "jane.smith@example.com" (same value every run)
};

No code? Use our visual JSON Generator.

Define your schema, pick your data types, set the count, and download realistic mock JSON in seconds. No Faker.js setup required.

Open JSON Generator

Generating Realistic Relationships

Real data has internal consistency: an email address matches a name, a city matches a state, a phone number has the right country code. Faker.js has locale support and contextual generators to maintain this consistency:

javascript
import { faker } from '@faker-js/faker';

function generateConsistentUser() {
  // firstName and lastName are used in both name and email
  const firstName = faker.person.firstName();
  const lastName = faker.person.lastName();

  // Location data from the same locale stays consistent
  const state = faker.location.state({ abbreviated: true });
  const city = faker.location.city();
  const zipCode = faker.location.zipCode({ state });

  return {
    name: `${firstName} ${lastName}`,
    email: faker.internet.email({ firstName, lastName }).toLowerCase(),
    location: { city, state, zipCode },
  };
}

Edge Cases to Include in Mock Data

Realistic mock data should include the awkward edge cases that production data always eventually contains:

  • Empty strings and null values: Test that your UI handles missing optional fields gracefully.
  • Maximum-length strings: If your database column is VARCHAR(255), generate some values that are exactly 255 characters.
  • Unicode and special characters: Names like `O'Brien`, `García`, `中文`, `العربية` expose encoding bugs.
  • Boundary dates: The Unix epoch (1970-01-01), dates far in the future, leap days (Feb 29).
  • Numeric edge cases: Zero, negative numbers, very large integers, floating-point numbers near zero.
  • Nested arrays with varying depths: Test that your tree rendering logic handles both shallow and deeply nested data.

JSON Schema-Based Generation

If you already have a JSON Schema that defines your data structure, you can generate mock data directly from it using `@faker-js/faker` combined with `json-schema-faker`:

javascript
import jsf from 'json-schema-faker';
import { faker } from '@faker-js/faker';

jsf.extend('faker', () => faker);

const schema = {
  type: 'object',
  properties: {
    id: { type: 'string', format: 'uuid' },
    name: { type: 'string', faker: 'person.fullName' },
    age: { type: 'integer', minimum: 18, maximum: 80 },
    email: { type: 'string', format: 'email' },
  },
  required: ['id', 'name', 'age', 'email'],
};

const mockUser = jsf.generate(schema);
console.log(JSON.stringify(mockUser, null, 2));

Testing Strategies with Mock JSON

  • Snapshot testing (Jest): Serialize mock data with a fixed seed and compare against a stored snapshot. Snapshot diffs reveal accidental data shape changes.
  • Property-based testing (fast-check): Generate thousands of random JSON inputs and verify that your parsing/processing code handles all of them correctly.
  • Contract testing (Pact): Define the JSON schema your API consumer expects, then generate mock responses from that schema to test both sides independently.
  • Load testing (k6, Artillery): Use Faker-generated JSON payloads in your load test scripts to simulate realistic API traffic rather than sending the same payload repeatedly.

Generate Mock JSON Data Instantly

Our JSON Generator produces realistic fake data: names, emails, UUIDs, dates, addresses, and more, in any schema you need.

Open JSON Generator

Related Articles