Home Code A Pretty Approach to Parse Gherkin Feature File to JSON

A Pretty Approach to Parse Gherkin Feature File to JSON

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 JSON Formatter 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) {
} catch (RuntimeException e) {

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);
System.out.println(“json output: n” + json + “‘”);

// Finally flush and close
try {
FileWriter file = new FileWriter(jPath);
} catch (IOException e) {

long endTime = System.currentTimeMillis();
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 the option to select pretty or ugly output format.

Disclaimer: MashTips is supported by its audience. When you purchase through links on our site, we may earn an affiliate commission at no extra cost to you.

Free Newsletter


Privacy Settings on iOS 13/iPadOS: What’s new?

Have you ever thought why people are so much crazy towards iPhones? Besides user-friendliness and tons of features, Apple always gives importance to data...

Apple CarPlay: How to Set up and Work with CarPlay?

Apple CarPlay is another smart driving infotainment system that keeps you connected with minimal distraction while driving. Similar to Android Auto, CarPlay also mirrors mobile...

Zephyr Sweat Proof Gaming Mouse with Cooling Fan and RGB Light

There are tons of gaming mice come in all shapes and sizes. Some even have craziest of the features. However, most of them, at...

Elevate Command Prompt in Windows 8-Error 740 Resolved

Are you getting “Error 740: Elevated permissions are required to execute” message while issuing commands through Windows 8 Command Prompt? Probably you are not...

Home Security System Buying Guide-6 Things you Need Consider

Technological advancements have caused a rise in almost all industrial and corporate sectors, such as Home security systems as well. Compare to a single...

Best Trick to Find out Local Data Providers in your area.

Hold on your plan to buy your data card for smart phone. Are you sure you are going to get enough signal strength and speed...