Gerador de Dados Mock JSON

Gere dados mock JSON realistas a partir de schema ou exemplo com seed configurável

What is a JSON Mock Generator?

A JSON Mock Generator produces realistic, randomized JSON data instances that conform to a given JSON Schema definition. Rather than manually crafting test fixtures or writing custom faker scripts, you provide a schema describing your data structure — including types, formats, constraints, enumerations, and required fields — and the generator outputs valid JSON objects that satisfy every declared rule. This schema-driven approach ensures that generated data respects type constraints, string patterns, numeric ranges, array lengths, and property dependencies exactly as specified. The result is mock data that accurately reflects your API contract, making it immediately useful for integration testing, frontend prototyping, documentation examples, and load testing without requiring a running backend.

How Schema-Driven Generation Works

JSON Schema is a declarative language for describing the structure and validation rules of JSON documents. A JSON Mock Generator reads the schema and traverses its structure recursively, generating values for each node based on its declared constraints. The generation process handles the full spectrum of JSON Schema keywords:

  • Type constraints: type determines whether to generate a string, number, integer, boolean, array, object, or null value.
  • String rules: minLength, maxLength, pattern (regex), and format (email, uri, date-time, uuid, ipv4, hostname, etc.) guide string generation.
  • Numeric bounds: minimum, maximum, exclusiveMinimum, exclusiveMaximum, and multipleOf constrain generated numbers.
  • Array rules: minItems, maxItems, uniqueItems, and items schema control array generation.
  • Object rules: required, properties, additionalProperties, minProperties, and maxProperties define object structure.
  • Enumeration: enum restricts output to one of the listed values, selected at random.
  • Composition: oneOf, anyOf, and allOf combine sub-schemas for complex type unions and intersections.
  • References: $ref resolves internal definitions, enabling reuse of shared schema fragments.

By interpreting these keywords, the generator produces data that would pass validation against the same schema. This is fundamentally different from generic fake data generators that produce random values without understanding the structural relationships between fields or the constraints imposed by a formal schema definition.

Use Cases for Schema-Based Mock Data

Schema-driven mock generation solves several common development challenges where realistic, structurally correct test data is needed but a live data source is unavailable or impractical:

  • API testing without a backend: Frontend developers can generate mock API responses directly from the OpenAPI/Swagger schema, enabling UI development in parallel with backend work. The generated data respects all field types, required properties, and format constraints defined in the API specification.
  • Integration and contract testing: Verify that your application correctly handles all valid data shapes by generating edge-case instances. A schema with minimum: 0 and maximum: 100 will produce boundary values that expose off-by-one errors and type coercion bugs.
  • Documentation examples: Automatically generate realistic request/response examples for API documentation. Since the data conforms to the schema, examples are always valid and consistent with the documented contract.
  • Database seeding and load testing: Generate hundreds or thousands of valid records for populating development databases or stress-testing endpoints with diverse, realistic payloads that exercise different code paths.
  • Schema validation feedback: By generating instances from your schema and inspecting the output, you can verify that your schema actually describes the data shapes you intend — catching overly permissive or restrictive rules before they reach production.

Understanding JSON Schema Formats and Patterns

The format keyword provides semantic hints for string generation beyond simple length constraints. When a schema declares "format": "email", the generator produces strings matching the RFC 5322 email address structure (e.g., user.name@example.com) rather than arbitrary characters. The JSON Schema specification defines several standard format values:

  • date-time — ISO 8601 date-time (e.g., 2024-03-15T09:30:00Z)
  • date — ISO 8601 full date (e.g., 2024-03-15)
  • time — ISO 8601 time (e.g., 09:30:00Z)
  • email — Internet email address per RFC 5322
  • uri — Universal Resource Identifier per RFC 3986
  • uuid — UUID per RFC 4122 (e.g., 550e8400-e29b-41d4-a716-446655440000)
  • ipv4 — IPv4 address (e.g., 192.168.1.100)
  • ipv6 — IPv6 address (e.g., 2001:db8::1)
  • hostname — Internet hostname per RFC 1123

The pattern keyword allows even more specific control via regular expressions. A schema declaring "pattern": "^[A-Z]{2}-\d{4}$" will generate strings like AB-1234 or XY-9087 that match the regex constraint. This is particularly useful for generating identifiers, product codes, or locale-specific formats that follow a known pattern structure.

Differences from Template-Based Fake Data Generators

It is important to understand the distinction between schema-driven mock generation and template-based fake data generation. A template-based fake data generator (such as Faker.js) produces random values based on field name heuristics or explicit template calls — for example, generating a random name when it encounters a field called firstName. This approach works well for quick prototyping but has significant limitations:

  • No contract enforcement: Template generators have no concept of required fields, numeric bounds, or string patterns. They cannot guarantee that output matches a specific schema contract, meaning generated data may be rejected by validators or cause unexpected behavior in systems that expect strictly typed payloads.
  • No composition handling: JSON Schema supports oneOf, anyOf, allOf, and conditional schemas (if/then/else). Schema-driven generators understand these combinators and produce data that satisfies the composition logic, while template generators cannot reason about type unions or conditional constraints.
  • No reference resolution: Schemas often use $ref to define reusable components (like shared address or pagination schemas). A schema-driven generator resolves these references and generates consistent data across all usages, maintaining referential integrity.
  • Schema as single source of truth: When your API schema changes, the mock generator automatically produces data matching the new structure. Template-based generators require manual updates to match the new contract.

In practice, these tools complement each other. Use schema-driven generation when you need data that conforms to a formal contract (API testing, contract validation). Use template-based generators when you need human-readable sample data without a schema (quick UI mockups, demo environments).

Code Examples

JSON Schema input and generated mock data output

// Input: JSON Schema defining a User object
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "type": "object",
  "properties": {
    "id": {
      "type": "integer",
      "minimum": 1,
      "maximum": 99999
    },
    "username": {
      "type": "string",
      "minLength": 3,
      "maxLength": 20,
      "pattern": "^[a-z][a-z0-9_]+$"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
    "age": {
      "type": "integer",
      "minimum": 18,
      "maximum": 120
    },
    "role": {
      "type": "string",
      "enum": ["admin", "editor", "viewer"]
    },
    "tags": {
      "type": "array",
      "items": { "type": "string", "minLength": 2 },
      "minItems": 1,
      "maxItems": 5,
      "uniqueItems": true
    },
    "isActive": {
      "type": "boolean"
    }
  },
  "required": ["id", "username", "email", "role", "isActive"]
}

Output:

// Output: Generated mock data conforming to the schema
{
  "id": 4527,
  "username": "jdoe_dev42",
  "email": "maria.chen@example.org",
  "age": 34,
  "role": "editor",
  "tags": ["typescript", "testing", "api"],
  "isActive": true
}

Standards & Specifications