/jg

JSON generator

Primary LanguageGo

jg

jg is a JSON generator written in Go.
It can generate JSON documents of any complexity from human-friendly YAML schema.

Why another JSON generator?

There are some other JSON generators:

Unfortunately, these generators are not flexible enough in strings generation. They support only a fixed range of categories (e.g. Name, City, Country, etc...) which are internally selected appropriate wordlist to take strings from. So there is no way to manually pass a dictionary to these generators.

jg supports passing external dictionaries with files flag.

Usage

λ jg --help
Usage: jg [OPTIONS] SCHEMA

JSON generator

Options:
  -a, --array [min,]max         Generate array of root objects (0 means do not wrap in array)
  -f, --files stringToString    Bind files to their names in schema (default [])
  -n, --nosort                  Do not sort keys in objects
  -o, --output string           JSON output (default "/dev/stdout")
      --output-buff-size uint   Buffer size for JSON output (0 means no buffer) (default 1024)
  -s, --stream int              Stream root objects delimited by newline (-1 means endless)

Install

At the moment, only installing by compiling source code is available. So you should have Go installed.

# clone it outside GOPATH
go get github.com/mitinarseny/jg

# get dependencies using go modules (needs go 1.11+)
go get ./...

# build
go build -o jg ./cmd

# check it works
./jg --help

Schema

Schema is defined in YAML format. Here is a small example:

files:
  file1:

root:
  type: object
  fields:
    field1: int
    field2:
      type: string
      from: file1

See examples for more.

There are two top-level fields:

  • root: node
  • files: object: mapping with file names.
    These names are not real paths in file system. They can be mapped to real files with files CLI argument.
    Each file must contain strings separated with newline character \n.
    files:
      file1:
      file2:
      # ...
      fileN:

Each node (even root) must specify its type with type field:

root:
  type: object
  fields:
    field1:
      type: int

Types

Here is the list of supported node types:

Types bool, int and float can be inlined. In this case, the defaults are applied for each type correspondingly.

boolInline: bool
boolExplicit:
  type: bool

integerInline: int
integerExplicit:
  type: int
  range: [0, 100]

floatInline: float
floatExplicit:
  type: float
  range: [0, 1]

bool

A boolean value. It simply generates true or false in output JSON.

int

An integer number. It can have only one of possible fields:

  • range: {int | [int, int]} (default [0, 10])
    Range of posssible values (with maximum included). It can be one of the following types:
    • int: equivalent to [0, int]
      range: 10 # [0, 10]
    • [int, int]: minimum and maximum correspondingly
      range: [0, 10]
  • choices: []int Possible choices. Example:
    choices: [2, 3, 5, 7, 11, 13, 17, 19]

float

An floating-point number. It can have only one of possible fields:

  • range: {int | [float, float]} (default [0, 1])
    Interval of posssible values (with maximum excluded). It can be one of following types:
    • float: equivalent to [0, float]
      range: 7.7
    • [float, float]: minimum and maximum correspondingly
      range: [5.2, 11.3]
  • choices: []float Possible choices. Example:
    choices: [3.14, 2.71, 4.20]

string

A string value. It must specify one of the following fields:

  • from: string: name of file to take strings from. This name should be listed in files top-level field.
    files:
      someFile:
    
    root:
      stringFromFile:
        type: string
        from: someFile
  • choices: []string Possible choices. Example:
    choices:
      - choice 1
      - choice 2
      - choice 3

array

An array object. It must specify its elements.

  • elements: node Defines an element of array. It can be node of any type.
  • length: {uint | [uint, uint]} (default: 10) Length of array to generate. It can be one of the following types:
    • uint: exact length of array
      length: 10
    • [uint, uint]: minimum and maximum of length correspondingly
      length: [0, 10]

object

An object. It must specify its fields:

  • fields: object: mapping of field names to nodes. Example:
    type: object
    fields:
      a: int
      b: float