Runscope API Testing and Monitoring    Learn More →

JSON Generator: Create Random, Structured JSON Mock Data with Finesse

By Neil Mansilla on .

Have you ever needed to craft mock data for API or script testing? Creating a single record of semi-realistic structured data isn’t that hard.. but do it at scale, with say, thousands of records can be a daunting task. For instance, I was creating API-driven puzzles for a contest we were running at a tech conference (AWS re:Invent). One of the puzzles required 100+ fictitious personnel records with a sequential index, random guid and fictitious names.

Enter JSON Generator (json-generator.com), a tool that handles the heavy lifting for generating structured JSON objects, stuffed with sequenced and/or controllable random data. Lets dive right in with the example of how I used it for the Runscope challenge question at the conference. 

A Simple Template

JSON Generator has an easy, but powerful templating language. Below is the template I used to generate a roster of 100 random people for the puzzle:

Breaking it down:

  • Line 2: roster is the key to our top-level object, which is an array. This isn't a function, it's just what I named the array.
  • Line 3: {{repeat(100)}} is an instruction to repeat an array item a number of times. In this case, it's going to repeat the object below 100 times. If we had specified {{repeat(10,50)}},   the array item would be generated somewhere between 10 and 50 times (random number).

  • Line 5: {{index(0)}} makes this field an index, and in this case, the starting index number is 0. The next record in the array will be 1, and so on. (integer type)

  • Line 6: {{guid()}} generates a random globally unique identifier (string)

  • Line 7: {{firstName()}} {{surname()}} generates random first names and last names.

NOTE: The quotes around key names is optional in the template format. In the generated data, required quotes will appear, which is JSON compliant.

Generated Roster

After clicking the Generate button, the JSON object was created based on the template above. Below is a screen shot containing just the first two objects in the array (there were 100 generated):

In this example, I only used four of the many functions that are available on JSON Generator. There are well over a dozen more functions to help you generate structured random JSON objects. A few of my favorites are:

  • lorem - random Lorem Ipsum text, configurable by the type units (words, sentences, paragraphs) and the number of units.
  • street, city and state - US street, city and state names — and you can choose between full and abbreviated state names.
  • floating - floating point numbers (decimals) between min and max values. Various formats available as supported by Numeral.js.
  • random - given a set of array values, this function will select one of them at random
  • date - given start and end date, will select a random date. Formatting as supported by datef, including ISO-8601. 

It’s not only fun to play with, but it could save you hours of tedious work when you need to generate heaps of mock JSON data. Don’t take my word for it. Give it a whirl. In fact, we liked JSON Generator so much that we decided to sponsor it!

TIP: Save versions of your templates locally as you work.

Categories: howto, community, testing

Everything is going to be 200 OK®