Skip to content

🎩 Generate JSON/YAML/XML... mock data with a structured template

License

Notifications You must be signed in to change notification settings

KevCui/Fakergen

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Fakergen

Generate JSON/YAML/XML... mock data with a structured template using Faker under the hood.

Table of Contents

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

$ 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:
$ 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.

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 can simply do the job. For example, repeat attributes:
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:

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 is recommended.

Credits

Inspired by JSON Generator


Buy Me A Coffee