My name is Carl. I am a software engineer and architect. I blog about C#, .NET, Javascript, AngularJS, nodejs, and more. Thanks for visiting!



Buy me a coffeeBuy me a coffee

Tags

Create Fake Data Using Json Schema Faker

In this post I will look at how to create fake data using Json Schema Faker and faker.js. Json Schema Faker is a node package that uses fake data generators and a schema defined in JSON to create fake data. Any application that handles data in the JSON format can use this for testing, prototyping, or general development. I will cover how to create a json schema and how to generate mock data for it using Json Schema Faker. This tutorial assumes basic knowledge of javacsript, NodeJS, and NPM and that NodeJS and NPM are installed.

The mock data will represent a list of web bookmarks. The data we want to include for each bookmark is an id, title, url, and comma dilimitted list of tags. The first thing to do is install Json Schema Faker and faker.js using npm.


 npm install json-schema-faker
 npm install faker


The next step is to create the javascript file that will define and save the fake data. Create a file named 'generateData.js'. First, we bring in the dependencies we need.

const jsf = require('json-schema-faker'); // json-schema faker
const fs = require('fs'); // nodeJS file system library to read and write files


Below is what the final schema definition looks like. I'll go over each part in detail below.

var bookmarksSchema = {
    "type": "object",
    "properties": {
      "bookmarks": {
        "type": "array",
        "minItems": 3,
        "maxItems": 5,
        "items": {
          "type": "object",
          "properties": {
            "id": {
              "type": "number",
              "unique": true,
              "minimum": 1
            },
            "url": {
              "type": "string",
              "faker": "internet.url"
            },
            "title": {
              "type": "string"
            },
            "tags": {
              "type": "string",
              "faker":"custom.tags"
            }
          },
          "required": ["id", "url", "title", "tags"]
        }
      }
    },
    "required": ["bookmarks"]
  };

The first line, var bookmarksSchema = { defines a regular javascript object variable named bookmarksSchema. This gets set equal to a javascript object that contains the schema definition for the bookmarks. The first line says "type": "object",, so we're creating an object. Next, the properties of this object are listed. In this case we want 1 property named "bookmarks". We define bookmarks as an array that contains between 3 and 5 items. Each item within that array is also an object. Each of those objects has 4 properties, id, url, title, and tags. Finally, we say that all 4 properties are required using "required": ["id", "url", "title", "tags"]. To close it out we declare which properties of the schema are required. In this case we only have 1 property, bookmarks, so we make it required. That is the last line "required": ["bookmarks"].

Each property of the bookmark item has it's own definition to tell Json Schema Faker how to generate data for it. We want id to be a number, that is unique, and starts with 1. We want url to be a string and we pass a hint that we want it to be a string that looks like a url, not just any string. We want title to be a string and any string will work. Finally, we define tags and this is a little more interesting. We want tags to be a string but we want it to be a comma delimitted list of strings. To do this, I'm going to extend Json Schema Faker to use a custom property. This is defined in the schema definition like this "faker":"custom.tags"

The code that defines custom.tags is added above the bookmarksSchema variable. I'm extending Json Schema Faker by defining a custom object that contains a function named tags. I can reference the result of the funciton by calling custom.tags where it is needed. The function returns 3 random strings concatinated by commas. Since custom.tags is a function, it can be as complex as the developer wants. We could define a random number and have it return that many words. So some bookmarks could have 3 tags, some 2 tags, or 4 tags. This also means that any data can be faked because we could have this function do anything we want and return the result. Below is the code for extending Json Schema Faker. Place this code above the bookmarksSchema variable.

jsf.extend('faker', function(){
  var faker = require('faker');
  faker.locale = "en"; // or any other language
  faker.custom = {
    tags: function(length) {
      return faker.lorem.word() + "," + faker.lorem.word() + "," + faker.lorem.word();
    }
  };
  return faker;
});


Now we have a javascript object that defines a JSON schema and related information to generate fake data. We pass this object into jsf like this var fakeData = jsf(bookmarksSchema);. Now the fake data is a javascript object defined as fakeData. Typically, we need to use the fake data outside where it was generated so I'm going to use the filesystem api of NodeJS to save the fake data to a file so it can be used in other applications. The code below creates a file named bookmarks.json with the fake data written to it.

fs.writeFile("bookmarks.json", JSON.stringify(fakeData), function (err) {
  if (err) {
    return console.log(err);
  } else {
    console.log("Mock data generated.");
  }
});


The entire generateData.js file is listed below.

const jsf = require('json-schema-faker'); // json-schema faker
const fs = require('fs'); // nodeJS file system library to read and write files

jsf.extend('faker', function(){
    var faker = require('faker');
    faker.locale = "en"; // or any other language
    faker.custom = {
      tags: function(length) {
        return faker.lorem.word() + "," + faker.lorem.word() + "," + faker.lorem.word();
      }
    };
    return faker;
  });
  

var bookmarksSchema = {
    "type": "object",
    "properties": {
      "bookmarks": {
        "type": "array",
        "minItems": 3,
        "maxItems": 5,
        "items": {
          "type": "object",
          "properties": {
            "id": {
              "type": "number",
              "unique": true,
              "minimum": 1
            },
            "url": {
              "type": "string",
              "faker": "internet.url"
            },
            "title": {
              "type": "string"
            },
            "tags": {
              "type": "string",
              "faker":"custom.tags"
            }
          },
          "required": ["id", "url", "title", "tags"]
        }
      }
    },
    "required": ["bookmarks"]
  };

  var fakeData = jsf(bookmarksSchema);
  fs.writeFile("bookmarks.json", JSON.stringify(fakeData), function (err) {
    if (err) {
      return console.log(err);
    } else {
      console.log("Mock data generated.");
    }
  });

To run the example locally, copy and paste the package.json file below and run npm install in the terminal. This will download the required NPM dependencies and then run node generateData.js to generate a json file with fake data.

{
  "name": "json-schema-faker-example",
  "version": "1.0.0",
  "description": "",
  "author": "Carl Layton",
  "license": "MIT",
  "dependencies": {
    "faker": "^4.1.0",
    "json-schema-faker": "^0.5.0-rc13"
  }
}


In conclusion, creating fake data with Json Schema Faker doesn't take much effort or code. The majority of the work is done in defining the schema. faker.js supports a lot of different kinds of fake data out of the box and I suggest reviewing the documentation to see what other kinds of data can be easily faked. It is a great way to get fake data for UI prototyping or front end web development before an API is created.




If you enjoyed this article...

Buy me a coffeeBuy me a coffee



© 2025 Carl Layton Creative Commons License Disclaimer
An unhandled error has occurred. Reload 🗙