Spark – Read JSON file to RDD

JSON has become one of the most common data format that is being exchanged between nodes in internet and applications.

In this tutorial, we shall learn how to read JSON file to an RDD with the help of SparkSession, DataFrameReader and DataSet<Row>.toJavaRDD().

Steps to Read JSON file to Spark RDD

To read JSON file Spark RDD,

  1. Create a SparkSession.
    SparkSession spark = SparkSession
    		.appName("Spark Example - Write Dataset to JSON File")
  2. Get DataFrameReader of the
  3. Use DataFrameReader.json(String jsonFilePath) to read the contents of JSON to Dataset<Row>
  4. Use Dataset<Row>.toJavaRDD() to convert Dataset<Row> to JavaRDD<Row>

Example : Spark – Read JSON file to RDD

Following is a Java Program to read JSON file to Spark RDD and print the contents of it.


{"name":"Michael", "salary":3000}
{"name":"Andy", "salary":4500}
{"name":"Justin", "salary":3500}
{"name":"Berta", "salary":4000}
{"name":"Raju", "salary":3000}

import org.apache.spark.sql.Row;
import org.apache.spark.sql.SparkSession;

public class JSONtoRDD {
	public static void main(String[] args) {
		// configure spark
		SparkSession spark = SparkSession
				.appName("Spark Example - Read JSON to RDD")

		// read list to RDD
		String jsonPath = "data/employees.json";
		JavaRDD<Row> items =;

		items.foreach(item -> {




In this Spark Tutorial, we have learnt to read JSON file to Spark RDD with the help of an example Java program.