Giter Site home page Giter Site logo

genjson's Introduction

GenJSON

A simple CLI used to produce a JSON stream with fake data using JSON schema.

This is basically a CLI upon json-schema-faker project.

Install

npm install -g genjson

Configuration

The JSON generation is based on a JSON-Schema (draft-04).

You can add an x-faker attribute to choose the data to be generated.

Example:

{
  "id": "Sample",
  "type": "object",
  "properties": {
    "uuid": {"type": "string", "x-faker": "random.uuid"},
    "timestamp": {"type": "integer", "x-faker": "timestamp.now"},
    "email": {"type": "string", "x-faker": "internet.email"}
  }
}

This will generate this kind of result:

{
  "uuid": "632ff1b6-4d5e-435d-a664-5d4185a6c78e",
  "timestamp": 1537174965817,
  "email": "[email protected]"}
}

Please refer to faker.js documentation to see available methods.

There is also some builtin generators:

  • "x-faker": {"custom.val": "hello world"}: A parameterized faker used to set a constant value.
  • "x-faker": "timestamp.now": Set current time using timestamp format.
  • "x-faker": "timestamp.iso": Set current time using ISO format.
  • "x-faker": "timestamp.recent": Set a timestamp from the recent past.
  • "x-faker": "timestamp.future": Set a timestamp form the future.

You can add you own generators (see extensions section).

Usage

Type genjson to see usage.

Example 1

$ genjson -c 10 -i 100 -o file://test.log -s ./schemas/sample.json
  • Generates:
    • 10 JSON objects: -c 10
    • 1 object every 100ms: -i 100
    • to test.log file: -o file://test.log
    • using sample schema: -s ./schema/sample.json

Example 2

You can use jq to properly display the JSON event.

$ genjson -s ./schemas/sample.json | jq .
  • Generates:
    • 1 JSON object
    • to the STDOUT
    • using sample schema: -s ./schema/sample.json

Example 3

$ genjson -c 100 -i 1000 -o https://requestb.in/13s7hxv1 -s ./schemas/sample.json
  • Generates:
    • 100 JSON objects: -c 100
    • 1 object every second: -i 1000
    • to an HTTP endpoint: -o https://requestb.in/13s7hxv1
    • using sample schema: -s ./schema/sample.json

Example 4

$ genjson -c 5 -i 1000 -s schemas/sample.json | xargs -l1 sh -c 'echo "$@" | http POST requestb.in/13s7hxv1'
  • Generates:
    • 5 JSON objects: -c 5
    • 1 object every second: -i 1000
    • using sample schema: -s ./schema/sample.json
    • to an HTTP endpoint using httpie: | xargs -l1 sh -c 'echo "$@" | http POST requestb.in/13s7hxv1'

Extensions

You can extend the generation of fake data by adding your own custom generator.

Example:

Create a new file (suffixed by .fake.js) into the custom-faker directory:

vi custom-faker/foo.fake.js

Export a function having a faker instance as parameter:

'use strict'

function Foo (faker) {
  this.bar = function (val) {
    return `FOO - {val}`
  }
}

module['exports'] = Foo

Note: this example of a faker function called bar having a parameter.

Now you can use this new faker into you JSON schema:

{
  "id": "Sample",
  "type": "object",
  "properties": {
    "id": {"type": "string", "x-faker": "random.uuid"},
    "sample": {
      "type": "string",
      "x-faker": {"foo.bar": "hello"}
    }
  }
}

You will get the following result:

{
  "id": "e6e53833-57e1-4a5b-aa04-7883bd29c57c",
  "sample": "FOO - hello"
}

genjson's People

Contributors

ncarlier avatar

Stargazers

Timothy Spann avatar Johan Euphrosine avatar Srinivasan Ragothaman avatar

Watchers

 avatar James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.