npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

genjson

v1.1.0

Published

CLI to generate fake JSON as a stream

Downloads

5

Readme

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"
}