5 releases (3 breaking)

Uses new Rust 2024

0.4.1 Jan 10, 2026
0.4.0 Jan 1, 2026
0.3.0 Dec 10, 2025
0.2.0 Apr 10, 2025
0.1.0 Sep 13, 2024

#167 in Template engine

MIT license

15KB
295 lines

rjg

rjg Actions Status License

Random JSON Generator.

rjg reads a JSON template from stdin and generates random JSON values based on embedded generator patterns.

// Install
$ cargo install rjg

// Generate integer arrays
$ echo '[0, "$u4", 9]' | rjg --count 3
[0,8,9]
[0,6,9]
[0,5,9]

// Generate objects
$ echo '{"put": {"key": "$s[3]", "value": {"$oneof": [null, "$u16"]}}}' | rjg --count 3
{"put":{"key":"cic","value":63308}}
{"put":{"key":"b36","value":10142}}
{"put":{"key":"9dj","value":null}}

// Print help.
$ rjg -h
Random JSON Generator

Usage: rjg [OPTIONS]

Options:
  -h, --help            Print help ('--help' for full help, '-h' for summary)
      --version         Print version
  -c, --count <INTEGER> Number of JSON values to generate [default: 1]
  -s, --seed <INTEGER>  Seed for the random number generator

Rules

  • Input templates can use JSONC format (JSON with comments), but the output is always plain JSON
  • Literal JSON values in a template are output exactly as written
  • The following patterns are treated as generators and will produce random values:
    • Strings beginning with $ (e.g., "$u8", "$i32", "$s[5]", "$seqno")
    • Objects with a single $oneof key
  • Generators produce output as follows:
    • "$seqno": Generates a sequence number starting from 0 and incrementing by 1 for each occurrence of $seqno during generation (even within a single JSON value)
    • "$u<bits>": Generates a random unsigned integer with the specified bit width (e.g., "$u8", "$u16"; 1-64 bits)
    • "$i<bits>": Generates a random signed integer with the specified bit width (e.g., "$i8", "$i32"; 1-64 bits)
    • "$s[<length>]": Generates a random alphanumeric string of the specified length (e.g., "$s[5]"; minimum 1 character)
    • {"$oneof": [VALUE, ...]}: Randomly selects and outputs one value from the provided array

Dependencies

~1MB
~18K SLoC