Dummy JSON generator for Node.js

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.

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

  "people": [
    {{#repeat 2}}
      "id": {{index}},
      "firstName": "{{firstName}}",
      "lastName": "{{lastName}}",
      "email": "{{email}}",
      "work": "{{company}}",
      "age": {{number 20 50}},
      "dob": "{{date}}"
      "optedin": {{boolean}}
  "images": [
    {{#repeat 3 6}}
  "revision": {{uniqueIndex}},
  "tolerance": {{number '0' '2'}},
  "timestamp": "{{time}}"
  "people": [
      "id": 0,
      "firstName": "Leanne",
      "lastName": "Flinn",
      "email": "leanne.flinn@unilogic.com",
      "work": "Unilogic",
      "age": 26,
      "dob": "1987-04-15"
      "optedin": true
      "id": 1,
      "firstName": "Edward",
      "lastName": "Young",
      "email": "edward.young@solexis.com",
      "work": "Solexis",
      "age": 31,
      "dob": "1982-06-24"
      "optedin": false
  "images": [
  "revision": 0,
  "tolerance": 1.7508240924216807,
  "timestamp: "17:25"

Install via npm:

npm install dummy-json

The simplest way to use dummy-json is by giving it a string:

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

The more convenient way to use dummy-json is to load 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);

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

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

A common use of Dummy JSON is to create a mock API service that returns random data. 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(reqres) {
  res.set('Content-Type', 'application/json');

If you install dummy-json globally with npm install -g dummy-json you can use it from the command line to parse files:

dummy-json template.hbs > output.json

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 
{{#repeat 5 10}} // Repeats the block a random number of times between 5 and 10 
{{#repeat animals}} // Loops over array provided in the data options of parse() 

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. 

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 

Generates a random true or false boolean value.

Generates a random first name, from a predefined list.

Generates a random last name, from a predefined list.

Generates a random company name, from a predefined list.

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.

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.

Generates a random date between the two given values using the format YYYY-MM-DD. If no date is given it will generate a random one between 1900-01-01 and 1999-12-31.

{{date}} // Generates a random date between 1900-01-01 and 1999-12-31 
{{date '2014-01-01' '2014-02-01'}} // Generates a random date between the two values 

Generates a random time between the two given values using the format hh:mm. If no time is given it will generate a random one between 00:00 and 23:59

{{time}} // Generates a random time between 00:00 and 23:59 
{{time '09:00' '17:00'}} // Generates a random time between the two values 

The generated dates and times are passed through helper methods before finally being used. You can register custom formatters like so:

Handlebars.registerHelper('dateFormatter', function(date) {
  return date.getFullYear(); // Return your own formatted date here 
Handlebars.registerHelper('timeFormatter', function(date) {
  return date.getHours(); // Return your own formatted time here 

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:

var helpers = {
  orientationfunction(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.

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.

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.

It's even possible to separate your entities/resources in small pieces of code to promote reuse and encapsulation.

var personTmpl = {
  "id": {{index}},
  "firstName": "{{firstName}}",
  "lastName": "{{lastName}}",
  "email": "{{email}}",
  "work": "{{company}}",
  "age": {{number 20 50}},
  "optedin": {{boolean}}
var partials = {
  person: personTmpl
var template = {
  "people": [
    {{#repeat 3}}
      {{> person }}
var result = dummyjson.parse(template, {partials: partials});