Test Data with JSON

What is json?:

JSON is a lightweight data-interchange format. JSON is completely language agnostic, making it the ideal interchange format.It is also a subset of JavaScript’s Object Notation (the way objects are built in JavaScript). JSON is extensively used in web services ever and in many cases is replacing XML as data-interchange format.

How is it relevant to test data ?

We can maintain our test data in json format too (instead of excel or other formats) and that makes the performance relatively faster [if we are talking about extremely large # of files and data]. Whether we generate data or not, at least as a consumer of data from web services, applications and other integrating systems, we might need to understand how json works and be ready to have the capabilities to parse json with all CRUD operations as needed.


  1. JSON overview
  2. Options available in Ruby Stack
  3. JSON serialization and deserialization
  4. Cucumber Feature and Code

1) JSON overview:

JSON is built on two structures:

  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence



The visual depictions here pretty much sum up what a JSON is. Thanks to JSON.org.  Another quick round of summary that is relevant to Automation is:

  • An object is JSON is (key,value) pair [or multiple key,value pairs]
  • Key is a String. value can be String, json, object, array and so on

2) Python Options:

The library “json” converts JavaScript JSON format to/from Python nested dictionary/list. Basically, JavaScript array is Python’s list, and JavaScript object is Python’s dictionary.

  • json.dumps(obj) → Convert Python nested dictionary/list to JSON (output as string).
  • json.loads("json") → Convert JSON string into Python nested dictionary/list.

For reading/writing to file, use:

  • json.dump(python obj, file thing) → Convert Python nested dictionary/list to JSON (output to file-like object)
  • json.load("json", file thing) → Convert JSON string into Python nested dictionary/list and write into a file.

3) Serialization and de-serialization:

Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file. Its main purpose is to save the state of an object in order to be able to recreate it when needed. The reverse process is called deserialization.

An example of serialization is to retrieve a web service response body as json and save it to a file as json string.

An example of deserialization would be to read a json string from a file and pass it as request body when making a web service call

Entire behave Feature:

  • The first scenario parses a json string and depicts how to access different (key, value) pairs at different levels in the object hierarchy
  • The second scenario parses another json string which has arrays
  • The third scenario creates a json and saves it to a file (Serialization)
  • The fourth scenario accesses the json from file and uses it in program (deserialization)
  • The fith scenario is an application of json to a web UI Automation scenario. This example does the same actions of filling a form with data sets, the same example what we explained for Test Data with Excel 
  • Download the files and put in /features/support/data directory: practiceform.json , sample.json

Step Definitions: