<!--
  canonical-url: https://www.avonture.be/blog/json-faker
  published:     2024-11-19
  This is a static plain-Markdown mirror generated at build time.
  Visit the canonical URL above for the fully rendered page, with images and interactive components.
-->

# JSON - Faker & Mockup

> Generate and validate JSON data using Python's Faker library and Mockaroo. Learn to create fake data for testing and ensure file quality with schemas.

---
![JSON - Faker & Mockup](/img/v2/json.webp)

**TL;DR**

This article covers generating and validating fake JSON test data: using Python's `Faker` library to script custom fake records, using Mockaroo.com (schema builder + free API) to generate large datasets like 1,000+ rows, and using an online JSON-to-schema converter with Python's `jsonschema` library to validate that received files match the expected structure.

I recently worked on an ETL project in Python. Among other things, the script had to process JSON files that users dropped into a specific folder.

As this was a sensitive application, it was important to validate the script by submitting fake JSON files, but also to ensure the quality of the files received.

*Two companions when you work with JSON on the command line: [The jq utility for Linux](/blog/linux-jq) to inspect and filter a generated file, and [Rendering a JSON file as a mind map](/blog/json-crack) to grasp the structure of an unfamiliar one at a glance.*

For the fake files, I used a tool like the Faker library for Python.

## Generate faker JSON file

So the idea is to generate a dictionary (a json file) with fake data. Using the `Faker` library, it's really, really easy.

First install the library using `pip install faker`.

Below is a small Python script to generate fake data in French (just replace `range(1)` with `range(100)`, for example, to get 100 records):

**fake.py**

```python
fake = Faker()

fake = Faker('fr_FR')

data = []

for _ in range(1):
    user = {
        'firstname': fake.first_name(),
        'lastname': fake.last_name(),
        'address': fake.street_address(),
        'city': fake.city(),
        'country': fake.country(),
        'gender': fake.random_element(elements=('Homme', 'Femme', 'Non binaire'))
    }

    data.append(user)

# Pretty print the JSON data
print(json.dumps(data, indent=4, ensure_ascii=False))
```

![Faker in Python](./images/python.webp)

> **Information:**
>
> Continue your reading with the official Faker documentation: <https://faker.readthedocs.io/en/master/>

## Using Mockaroo.com

The <https://www.mockaroo.com/> website allows you to create fake data for free (to access certain functions, you will need to create a free account).

![Using Mockaroo](./images/mockaroo.webp)

### Creating a fake data using a schema

By creating a free account on Mockaroo, click on the `Schemas` button, then select `Create a schema`. In the next screen, click on `Generate fields using AI...` and paste an existing JSON string:

![Creating a schema](./images/creating_schema.webp)

This done, you'll be able to generate a large number of rows — for instance, a file with more than 1,000 records.

By saving examples to a real JSON file on disk, you can then use that file to test your application.

### Using API

By creating a free account on Mockaroo, you can obtain a free API key (allowing 200 requests/day).

The API documentation is here: <https://mockaroo.com/docs>.

Like previously, click on the `Schemas` button, create a schema and save it.

I've created the *schema\_test* like this:

![Mockaroo - Schema test](./images/mockaroo_schema_test.webp)

I can then use it in Python like this:

**schema\_test.py**

```python
# My API key as displayed in my profile page on Mockaroo
api_key = "MY_API_KEY"

# This is the name of my schema
schema = "schema_test"

# Number of records I want
num_records = 10

# Construct the API endpoint URL
url = f"https://api.mockaroo.com/api/generate?key={api_key}&count={num_records}&schema={schema}"

# Make the API request
response = requests.get(url)

# Check if the request was successful
if response.status_code == 200:
    # Parse the JSON response
    data = response.json()

    # Display the result on the console
    print(data)
else:
    print(f"Error fetching data: {response.text}")
```

> **Information:**
>
> To make this code work, remember to install the requests library: `pip install requests`.

By calling my script twice; each time I got a different set of data:

![Calling Mockaroo API](./images/calling_mockaroo_api.webp)

## Online JSON to Schema Converter

The <https://www.liquid-technologies.com/online-json-to-schema-converter> site will allow you to copy/paste existing JSON and get a skeleton of a JSON schema.

![Generate a schema](./images/generate_schema.webp)

Once you've the schema, you can use it in Python like this:

**validate.py**

```python
def validate_json(data_file, schema_file):
  with open(schema_file) as f:
    schema = json.load(f)

  with open(data_file) as f:
    data = json.load(f)

  jsonschema.validate(instance=data, schema=schema)

data_file = "data.json"
schema_file = "schema.json"

try:
    validate_json(data_file, schema_file)
    print("Data is valid!")
except jsonschema.exceptions.ValidationError as exception:
    print(f"Data is invalid: {exception}")
```

> **Information:**
>
> You'll need to run `pip install jsonschema` first.

Now, I'll test my file. The first time, my JSON file will be correct. I've then removed the `city` record from my JSON, and rerunning the script will indeed fail, as expected:

![JSON validation](./images/validate.webp)

> **Information:**
>
> As we can see, quite quickly, we've generated 1,000 records and, also, a validation schema. Then, using a few lines of Python, we've ensured the file is correct or, if not, we've spotted where the error is located.

Other JSON tools worth knowing about: [rendering a JSON file as a mind map](/blog/json-crack) and [linting/validating your JSON files](/blog/json-lint).
