dummy-json

Dummy JSON generator for Node.js

npm install dummy-json
1 downloads in the last day
11 downloads in the last week
38 downloads in the last month

Dummy JSON

Dummy JSON is a Node utility that allows you to generate random JSON data using Handlebars templates. It returns a JSON compatible string you can use in your app. It's useful for creating mock API services that return dummy data.

Example

For a complete list of helpers see the available helpers section.

Template
{
  "people": [
    {{#repeat 2}}
    {
      "id": {{index}},
      "firstName": "{{firstName}}",
      "lastName": "{{lastName}}",
      "email": "{{email}}",
      "work": "{{company}}",
      "age": {{number 20 50}},
      "optedin": {{boolean}}
    }
    {{/repeat}}
  ],
  "images": [
    {{#repeat 3 6}}
    'img{{index}}.png'
    {{/repeat}}
  ],
  "revision": {{uniqueIndex}},
  "tolerance": {{number '0' '2'}},
}
Output
{
  "people": [
    {
      "id": 0,
      "firstName": "Leanne",
      "lastName": "Flinn",
      "email": "leanne.flinn@unilogic.com",
      "work": "Unilogic",
      "age": 26,
      "optedin": true
    },
    {
      "id": 1,
      "firstName": "Edward",
      "lastName": "Young",
      "email": "edward.young@solexis.com",
      "work": "Solexis",
      "age": 31,
      "optedin": false
    }
  ],
  "images": [
    'img0.png',
    'img1.png',
    'img2.png',
    'img3.png'
  ],
  "revision": 0,
  "tolerance": 1.7508240924216807,
}

Getting started

Install via npm:

npm install dummy-json

Generate JSON

var dummyjson = require('dummy-json');
var template = '{ "name": {{firstName}}, "age": {{number 18 65}} }';
var result = dummyjson.parse(template);

Generate from a file

Instead of writing multi-line strings you can load the template from a file using Node's fs utility:

var fs = require('fs');
var dummyjson = require('./dummy-json');

var template = fs.readFileSync('template.hbs', {encoding: 'utf8'});
var result = dummyjson.parse(template);

Converting to JavaScript object

If there are no errors in the output then the returned string can be parsed into a JavaScript object:

var result = dummyjson.parse(template);
var obj = JSON.parse(result);

Using with a HTTP response

A common use of Dummy JSON is to create a mock API service that return random data you can test with. Here's a quick example using Express:

var fs = require('fs');
var express = require('express');
var dummyjson = require('./dummy-json');

var template = fs.readFileSync('template.hbs', {encoding: 'utf8'});
var app = express();

app.get('/people', function(req, res) {
  res.set('Content-Type', 'application/json');
  res.send(dummyjson.parse(template));
});

app.listen(3000);

Available helpers

{{#repeat [count/array] [maxCount]}} ... {{/repeat}}

Repeats blocks of content. Similar to Handlebars' built-in each, but adds commas between items and tidies up whitespace.

{{#repeat 4}} // Repeats the block exactly 4 times
"hello"
{{/repeat}}

{{#repeat 5 10}} // Repeats the block a random number of times between 5 and 10
"hello"
{{/repeat}}

{{#repeat animals}} // Loops over array provided in the data options of parse()
"{{this}}"
{{/repeat}}

You can print the current index of the loop using {{index}}. This is a helper that's only available within repeat blocks, (outside of a repeat block it will print undefined).

{{#repeat 4}}
"hello {{index}}" // "hello 1", "hello 2", etc.
{{/repeat}}

{{number [min/max] [max] [pad=true]}}

Generates a random number. If just one number is provided it will generate a number between 0 and the given number. The min and max values are inclusive in the generated number. Floats can be generated by wrapping the numbers in quote marks. The pad option pads the generated number with leading zeros (integers only).

{{number 20}} // Generates a random integer between 0 and 20
{{number 50 100}} // Generates a random integer between 50 and 100
{{number 50 100 pad=true}} // Pad integer with leading zeros, eg: 076
{{number '5.5' '8.5'}} // Generates a random float between 5.5 and 8.5

{{boolean}}

Generates a random true or false boolean value.

{{firstName}}

Generates a random first name, from a predefined list.

{{lastName}}

Generates a random last name, from a predefined list.

{{company}}

Generates a random company name, from a predefined list.

{{email}}

Generates a random email address, using the most recently printed name and company. This means it keeps in sync when used in conjunction with names and companies.

{{uniqueIndex}}

Generates a unique index that always increments by 1 each time it's used, regardless of whether it's inside or outside a repeat loop.

Advanced usage

The parse method accepts a second argument that allows you to configure the parsing routine. It's a plain object that can contain one or more of the following options:

Using your own Handlebars helpers

var helpers = {
  orientation: function(options) {
    return Math.random() > 0.5 ? 'left' : 'right';
  }
};
var template = '{ "position": {{orientation}} }';
var result = dummyjson.parse(template, {helpers: helpers});

Custom helpers can override built-in ones, which allows you to modify how the Available helpers work. For more information on writing helpers see the Handlebars documentation.

Using your own data

var data = {
  animals: ['cat', 'dog', 'cow', 'wolf', 'giraffe']
};
var template = '{ "pets": [ {{#repeat animals}}{{this}}{{/repeat}} ] }';
var result = dummyjson.parse(template, {data: data});

Useful for splicing bits of real data into the generated reponse. All the regular Handlebars functionality is available to work with the data.

Using your own list of names and companies

var firstNames = ['Frasier', 'Olivia', 'Marge', 'Holbeck'];
var lastNames = ['Crane', 'Dunham', 'Gunderson', 'Ghyll'];
var companies = ['KACL', 'Fringe', 'MPD'];
var template = '{ "name": {{firstName}}, "company": {{company}} }';
var result = dummyjson.parse(template, {
  firstNames: firstNames,
  lastNames: lastNames,
  companies: companies
});

Using your own names and companies will completely override the built-in collections. You can specify just one array, or all of them, as has been done above. Note: Names and companies loop when used repeatedly - to keep them in sync the length of the smallest array will be used as the loop point. In the example above the companies array is smallest and so the final first and last names won't ever appear.

npm loves you