JavaScript - - By Michael Wanyoike

Test Data JSON Example

With today’s modern coding practices, building a new application often requires a front-end and back-end building approach. Usually, two separate teams are assigned to work on each area simultaneously. In the early stages, front-end developers will need data to test the views they create. Back-end developers also need data to test CRUD logic, security, and other custom business processes that they are working on. In both cases, test data is often not available in the beginning. You could create some yourself, however, it is slow and often leads to inconclusive results.


July 27th, 2017: This article series was rewritten with up-to-date information and fresh examples.


Luckily, we live in an age where we can get access to online services that can easily generate hundreds of rows of test data for free. One such service is Mockaroo. It supports generating data in a number of data formats including JSON. Here is a sample I got from their website:

[{
  "id": 1,
  "first_name": "Jeanette",
  "last_name": "Penddreth",
  "email": "jpenddreth0@census.gov",
  "gender": "Female",
  "ip_address": "26.58.193.2"
}, {
  "id": 2,
  "first_name": "Giavani",
  "last_name": "Frediani",
  "email": "gfrediani1@senate.gov",
  "gender": "Male",
  "ip_address": "229.179.4.212"
}, {
  "id": 3,
  "first_name": "Noell",
  "last_name": "Bea",
  "email": "nbea2@imageshack.us",
  "gender": "Female",
  "ip_address": "180.66.162.255"
}, {
  "id": 4,
  "first_name": "Willard",
  "last_name": "Valek",
  "email": "wvalek3@vk.com",
  "gender": "Male",
  "ip_address": "67.76.188.26"
}]

The service provides an incredible 132 fields you can use to generate test data for your application. You can generate up to 1,000 rows of test data for free. There are commercial plans if you need to generate more.

If you are looking to have more control on the data being generated, there is another online service called json-generator that may have what you are looking for. It requires JavaScript input to output customized test data in JSON format. See the below example:

JavaScript Input:

[
  '{{repeat(5, 7)}}',
  {
    _id: '{{objectId()}}',
    isActive: '{{bool()}}',
    balance: '{{floating(1000, 4000, 2, "$0,0.00")}}',
    age: '{{integer(20, 40)}}',
    eyeColor: '{{random("blue", "brown", "green")}}',
    name: '{{firstName()}} {{surname()}}',
    gender: '{{gender()}}',
    company: '{{company().toUpperCase()}}',
    email: '{{email()}}',
    phone: '+1 {{phone()}}',
    friends: [
      '{{repeat(3)}}',
      {
        id: '{{index()}}',
        name: '{{firstName()}} {{surname()}}'
      }
    ],
    favoriteFruit: function (tags) {
      var fruits = ['apple', 'banana', 'strawberry'];
      return fruits[tags.integer(0, fruits.length - 1)];
    }
  }
]

JSON Test Data Output(partial results):

[
  {
    "_id": "5973782bdb9a930533b05cb2",
    "isActive": true,
    "balance": "$1,446.35",
    "age": 32,
    "eyeColor": "green",
    "name": "Logan Keller",
    "gender": "male",
    "company": "ARTIQ",
    "email": "logankeller@artiq.com",
    "phone": "+1 (952) 533-2258",
    "friends": [
      {
        "id": 0,
        "name": "Colon Salazar"
      },
      {
        "id": 1,
        "name": "French Mcneil"
      },
      {
        "id": 2,
        "name": "Carol Martin"
      }
    ],
    "favoriteFruit": "banana"
  }
]

The sample code shown on the front page shows the incredible number of ways you can customize the way data is generated.

Also: See more JSON examples.

Sponsors