Test Data JSON Example

Share this article

This test data JSON example is part of an article series that was rewritten in mid 2017 with up-to-date information and fresh examples.

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.

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.

Here are the other examples in this series:

FAQs on Leveraging JSON for Effective Data Testing and API Integration

What is JSON and why is it important in testing data?

JSON, or JavaScript Object Notation, is a lightweight data-interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition – December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.

In testing data, JSON is important because it allows testers to easily structure, generate, and manipulate expected data to validate the responses. It’s also widely used in APIs, which makes it a crucial aspect of modern web development.

How can I generate test data in JSON format?

There are several ways to generate test data in JSON format. One of the easiest ways is to use online tools like Mockaroo, which allows you to generate custom JSON data based on your specifications. You can define the fields, types, and range of data you want, and it will generate a JSON file for you.

Another way is to manually create your JSON data. JSON data is structured as a collection of key-value pairs. Here’s a simple example:

{
"name": "John Doe",
"age": 30,
"city": "New York"
}

In this example, “name”, “age”, and “city” are keys, and “John Doe”, 30, and “New York” are their corresponding values.

How can I test JSON properties in Postman?

Postman is a popular tool for API testing. It allows you to send HTTP requests and view responses, among other things. To test JSON properties in Postman, you can use the built-in test scripting feature.

Here’s a simple example. Let’s say you have an API that returns a user’s information in JSON format, and you want to verify the user’s name. You can write a test script like this:

pm.test("Check user name", function () {
var jsonData = pm.response.json();
pm.expect(jsonData.name).to.eql("John Doe");
});

In this script, pm.response.json() parses the response body to JSON, and pm.expect(jsonData.name).to.eql("John Doe") checks if the “name” property equals “John Doe”.

What are some common issues when working with JSON data and how can I troubleshoot them?

Working with JSON data can sometimes be tricky, especially when the data structure is complex. Some common issues include incorrect data types, missing keys, unexpected values, and syntax errors.

To troubleshoot these issues, you can use online JSON validators to check if your JSON data is correctly formatted. These tools can highlight syntax errors and help you fix them. If you’re dealing with incorrect data types, missing keys, or unexpected values, you may need to review your data generation logic or check the source of the data.

How can I use JSON data in my tests?

JSON data can be used in many ways in your tests. For example, if you’re testing an API, you can use JSON data as the request payload. You can also use JSON data to validate the response.

In unit tests, you can use JSON data as mock data to simulate different scenarios. This allows you to test your code’s behavior with different inputs without having to rely on a live data source.

In end-to-end tests, you can use JSON data to set up and tear down test data. For example, you can create a JSON file with a list of users, and use it to create and delete users before and after your tests.

Can I use JSON data in other formats like XML or CSV?

Yes, JSON data can be converted to and from other formats like XML and CSV. There are many online tools and libraries that can do this. For example, in JavaScript, you can use the JSON.parse() and JSON.stringify() methods to convert between JSON and string. There are also libraries like xml2js and csvtojson that can convert between JSON and XML or CSV.

How can I handle large JSON data in my tests?

Handling large JSON data in tests can be challenging, especially when it comes to performance and memory usage. One approach is to split the data into smaller chunks and process them one at a time. This can be done using streaming APIs or libraries that support streaming.

Another approach is to use a database or a data store that can handle large amounts of data. You can import the JSON data into the database, and then query the data as needed in your tests.

How can I compare two JSON objects in my tests?

Comparing two JSON objects in tests can be done using deep equality checks. Most testing frameworks provide functions or methods to do this. For example, in JavaScript, you can use the deepEqual() function from the assert module:

const assert = require('assert');
assert.deepEqual(object1, object2);

This will compare the two objects recursively and check if they have the same properties and values.

How can I generate random JSON data for my tests?

Generating random JSON data for tests can be done using libraries like Faker.js or Chance.js. These libraries provide functions to generate random data of various types, such as names, addresses, numbers, dates, and more. You can use these functions to generate random values for your JSON data.

Can I use JSON data in performance testing?

Yes, JSON data can be used in performance testing. For example, if you’re testing an API, you can use JSON data as the request payload and measure how the API performs with different sizes of data. You can also use JSON data to simulate different loads on the system, such as a large number of users or a high rate of requests.

Michael WanyoikeMichael Wanyoike
View Author

I write clean, readable and modular code. I love learning new technologies that bring efficiencies and increased productivity to my workflow.

jsonmocknilsonj
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week