JSON Chewer
TL;DR:
Generate random JSON file using pure javascript. It removes the complexity and inconsistences from creating string-based methods such as in madoka.
Benefits
- Write reliable code and avoid mistakes with real javascript code.
- Write complex algorithms in your own way.
- Use the libraries you need for the seed file.
- Use highlight from your favorite code editor.
- Use all Javascript (ES5 and ES6) features.
- Get rid of RegEx and eval methods to evaluate your own code.
- Use both API and CLI for generating files.
Instead of using string notations, you can write full javascript code:
// Madoka style company: '{{ company().toUpperCase() }}' // JSON Chewer style fakercompany
Installation
Install it locally or globally as a npm package:
# Local $ npm install --save json-chewer # Global $ npm install --global json-chewer
Getting started
- Create a Node.js file with the seed:
const faker repeat = ; moduleexports = users: ;
- Run the command line:
$ json-chewer my-seed.js
- Get the output:
IMPORTANT: There are differences between creating repeated values and creating repeated values into functions:
moduleexports = users: ;// Output example "user": "Mariela" "Mariela" "Mariela" "Mariela" "Mariela"
Scope inheritance
There is a scope management on each parsing step. The generator automatically modifies the children properties' functions to bind their parent scopes.
moduleexports = fakerinternet { // 'this' inherited from parent object return `http://example.com/`; } // Output example "username": "Verdie30" "profile": "http://example.com/Verdie30"
In case of arrays, it also binds the parent scope. However, it refers to the immediate parent object, not to the array:
moduleexports = fakercommerceproduct slogans: // Output example "name": "Chair" "slogans": "Ergonomic Chair" "Fantastic Chair" "Intelligent Chair"
IMPORTANT: Due to arrow function specifications, it's only possible to use this
from lexical environment if it's enclosed into a regular function:
moduleexports = fakerinternet { return thisusername // 'this' inherited from regular function } // Output example "username": "Alaina_Klocko70" "link": "profile": "Alaina_Klocko70"
Built-in features
Faker
It includes Faker as a library for generating fake data samples.
Check the Faker.js documentation before using it: https://github.com/Marak/faker.js/wiki.
It might be imported directly from the JSON Chewer:
const faker = ;
Repeat
There is a built-in function for generating arrays with specific values.
Repeated values
const repeat = ; moduleexports = foo: // Output example "foo": "bar""bar""bar"
Random values
const repeat = ; moduleexports = values: // Output example "values": 06866788951130716 01538252618213385 04480162893196198
Random range of items
const repeat = ; moduleexports = hello: // Output example "hello": "world" "world"
Random range of items with random values
const repeat = ; moduleexports = values: // Output example "values": 04480162893196198 01538252618213385
CLI
$ json-chewer Usage: json-chewer [options] <file> Generate random JSON file using pure javascript Options: -O, --output <file> Define the output file. -p, --pretty Use pretty formatting to output file. -v, --verbose Display additional information about the generation processing. -V, --version output the version number -h, --help output usage information