A Pretty Approach to Parse Gherkin Feature File to JSON

Last Updated: April 20, 2016

Gherkin is the plain English format that can understand  Cucumber to run the automation test.  Gherkin feature file language is business readable domain specific language.

This code is to convert your Gherkin feature file to JSON format based on JSONFormatter framework.

Gherkin to JSON Parser Main

//Gherkin feature file to JSON converted main class

public class GToJRun {
    // Here set the Gherkin path to fetch feature file and JSON file path to
    // write into JSON format
    private static String featurePath = "c:\Json\login.feature";
    private static String jasonPath = "c:\Json\login.json";

    // Set as pretty / ugly format for for JSON output. By default it is pretty
    static GtoJCore testG = new GtoJCore("pretty");

    public static void main(String[] args) {
        testG.gherkinTojson(featurePath, jasonPath);
    }
}

The core class is based on JSON formatter framework. You can get the converting time if you want to check the total processing time for a bigger feature file.

Gherkin to JSON Parser Core

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import gherkin.formatter.JSONFormatter;
import gherkin.formatter.JSONPrettyFormatter;
import gherkin.parser.Parser;
import gherkin.util.FixJava;

// Gherkin to Json parser core file.

public class GtoJCore {
	private String format;
	//To get the total running time (optional)
	long startTime = System.currentTimeMillis();

	public GtoJCore(String outFormat) {
		this.format = outFormat;
	}
	public String getOutFormat() {
		return format;
	}

	public void gherkinTojson(String fPath, String jPath) {

		// Define Feature file and JSON File path.
		String gherkin = null;
		try {
			gherkin = FixJava.readReader(new InputStreamReader(
					new FileInputStream(fPath), "UTF-8"));
		} catch (FileNotFoundException e) {
			System.out.println("Feature file not found");
			// e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (RuntimeException e) {
			e.printStackTrace();
		}

		StringBuilder json = new StringBuilder();
		JSONFormatter formatter;
		// pretty or ugly selection, pretty by default
		if (format.equalsIgnoreCase("ugly")) {
			formatter = new JSONFormatter(json);// not pretty
		} else {
			formatter = new JSONPrettyFormatter(json);// pretty
		}

		Parser parser = new Parser(formatter);
		parser.parse(gherkin, fPath, 0);
		formatter.done();
		formatter.close();
		System.out.println("json output: n" + json + "'");

		// Finally flush and close
		try {
			FileWriter file = new FileWriter(jPath);
			file.write(json.toString());
			file.flush();
			file.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

		long endTime = System.currentTimeMillis();
		//optional
		System.out.println("n Total Running Time: " + (endTime - startTime)
				+ " milliseconds");
	}
}

This provides you a pretty or ugly JSON format that you can select from the Main Class. You can define the file path both for feature file to read and JSON file to write with option to select pretty or ugly output format.