Matt Sweetman

Latest articles

Creating mock JSON data with dummy-json

Dummy JSON is a little Node utility I wrote over a year ago but have used countless times since. I thought I'd write about here in the hope that other people would take a look and perhaps help contribute. The github page for the project explains it best:

Dummy JSON is a Node utility that allows you to generate dynamic JSON data using Handlebars templates. It returns a JSON compatible string you can use in your app.

The following example explains it further, showing the input template and the generated output:

  "people": [
    {{#repeat 2}}
      "id": {{index}},
      "firstName": "{{firstName}}",
      "lastName": "{{lastName}}",
      "email": "{{email}}",
      "work": "{{company}}",
      "age": {{number 20 50}},
      "optedin": {{boolean}}
  "images": [
    {{#repeat 3 6}}
  "revision": {{uniqueIndex}},
  "tolerance": {{number '0' '2'}},
  "people": [
      "id": 0,
      "firstName": "Leanne",
      "lastName": "Flinn",
      "email": "",
      "work": "Unilogic",
      "age": 26,
      "optedin": true
      "id": 1,
      "firstName": "Edward",
      "lastName": "Young",
      "email": "",
      "work": "Solexis",
      "age": 31,
      "optedin": false
  "images": [
  "revision": 0,
  "tolerance": 1.7508240924216807,

The template on the left is written using Handlebars. The keywords enclosed in curly braces are custom Handlebars helpers written specifically for dummy-json. They generate random numbers, names, emails, and so on. Because they're just regular Handlebars helpers it means they're easily extensible, and adding functionality to dummy-json is extremely easy.

Using as a mock data source

This is perhaps the best way to use dummy-json, and something I've used several times to good effect. The idea is to set up a mock server that simulates your real API and use dummy-json to return mock JSON data. You can easily set up a local server using something like express. The following example shows you how easy this is to do:

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');


Future additions

There are many helpers I'd like to add, such as dates, addresses, and lists of countries. I'd also like to add a random seed so that the generated values can be consistent. If anyone is interested in contributing new ideas I'd be extremely happy! Take a look at the github page and check it out