https://github.com/kevcui/fakergen
:tophat: Generate JSON/YAML/XML... mock data with a structured template
https://github.com/kevcui/fakergen
data-generator devops devops-tools fake-generator faker faker-generator faker-providers json json-data json-generator json-schema json-template mock mock-data mock-json testing testing-tool testing-tools xml yaml
Last synced: 6 months ago
JSON representation
:tophat: Generate JSON/YAML/XML... mock data with a structured template
- Host: GitHub
- URL: https://github.com/kevcui/fakergen
- Owner: KevCui
- License: mit
- Created: 2020-08-01T12:29:37.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2021-05-10T11:13:43.000Z (almost 5 years ago)
- Last Synced: 2025-10-16T22:05:21.058Z (6 months ago)
- Topics: data-generator, devops, devops-tools, fake-generator, faker, faker-generator, faker-providers, json, json-data, json-generator, json-schema, json-template, mock, mock-data, mock-json, testing, testing-tool, testing-tools, xml, yaml
- Language: Python
- Homepage:
- Size: 24.4 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Fakergen
> Generate JSON/YAML/XML... mock data with a structured template using Faker under the hood.
## Table of Contents
- [Features](#features)
- [Dependency](#dependency)
- [Usage](#usage)
- [How to create a Faker template?](#how-to-create-a-faker-template)
- [How to create and use custom function?](#how-to-create-and-use-custom-function)
- [How to repeat certain elements?](#how-to-repeat-certain-elements)
- [Credits](#credits)
## Features
- Human-readable syntax as template
- Fully support Faker providers
- Customizable extended functions
- Support any file formats: JSON, YAML, XML...
- CLI script, easy to be integrated into any CI process
- Support seed value for generating same output data
## Dependency
```bash
$ pip install Faker
```
## Usage
```
usage: fakergen.py [-h] [-q] [-s SEED] template
positional arguments:
template template file
optional arguments:
-h, --help show this help message and exit
-q, --quiet be quiet, no error message
-s SEED, --seed SEED set seed value
```
- Generate JSON data using default `./template.json`:
```bash
$ cat ./template.json
[
{
"product_number": "{{bothify(text='????-########', letters='ABCDE')}}",
"hostname": "{{hostname(2)}}",
"attributes": [
{
"text": "static text",
"ean": "{{ean13(leading_zero=False)}}",
"random_number": {{pyint(min_value=1, max_value=999, step=1)}},
"colors": "{{color_name()}} {{color_name()}}",
"custom_greeting": "{{greeting()}}"
}
]
}
]
$ ./fakergen.py template.json
[
{
"product_number": "CEAD-00367795",
"hostname": "lt-75.collins.rivera-riley.com",
"attributes": [
{
"text": "static text",
"ean": "7263776026664",
"random_number": 65,
"colors": "OldLace Tomato",
"custom_greeting": "Hi there"
}
]
}
]
```
### How to create a Faker template?
Faker template is basically a file with variables inside. As an example, `./template.json` shows briefly how a template looks like: Using `{{...}}` to surround Faker provider name or custom function name will indicate the function to be executed as a Faker provider or a python function.
A list of Faker providers can be found [here](https://faker.readthedocs.io/en/stable/providers.html).
### How to create and use custom function?
The preprepared `CustomProviders` class is located in `./customprovider.py`. Firstly, add a function inside this class. Then this custom function can be called from Faker template.
Checkout `greeting()` function as an example.
### How to repeat certain elements?
- One solution is to generate a new JSON template with repeated elements. Using [jq](https://stedolan.github.io/jq/download/) can simply do the job. For example, repeat `attributes`:
```bash
jq '.[].attributes += $el' --argjson el "$(jq '.[].attributes' template.json)" template.json > newtemplate.json
```
If more repeats are needed, `for` loop is helpful. For example, repeat `attributes` 5 times:
```bash
j="$(cat template.json)"; for ((i=0;i<5;i++)); do j="$(jq '.[].attributes += $el' --argjson el "$(jq '.[].attributes' template.json)" <<< "$j")"; done; echo "$j" > newtemplate.json
```
To generate YAML/XML template, similar to `jq`, [yq](https://github.com/kislyuk/yq) is recommended.
## Credits
Inspired by [JSON Generator](https://www.json-generator.com/)
---
