Read JSON using PySpark

The JSON (JavaScript Object Notation) is a lightweight format to store and exchange data. The input JSON may be in different format —

  • simple,

Below will cover all the above scenarios:

1. Simple JSON:

Input JSON file (Simple.json) -

Code to read —

from pyspark.sql import SparkSession

spark = SparkSession.builder.config("spark.sql.warehouse.dir", "file:///C:/temp").appName("readJSON").getOrCreate()

readJSONDF ='Simple.json')

Output —

2. Multi-Line Complex JSON:

Input JSON file (ComplexJSON.json) — we will read Name and Location

Code to read-

To read multi-line JSON file we need to use option(“multiLine”,”true”). Also, in the above picture, the “details” tag is an array, so to read the content inside an array element we need to Explode it first. The below code will show how we can read Location and Name from the above input file

from pyspark.sql import SparkSession
from pyspark.sql import functions as F

spark = SparkSession.builder.config("spark.sql.warehouse.dir", "file:///C:/temp").appName("readJSON").getOrCreate()

# use multiline = true to read multi line JSON file
readComplexJSONDF ="multiLine","true").json('ComplexJSON.json')

# Explode Array to Structure
explodeArrarDF = readComplexJSONDF.withColumn('Exp_RESULTS',F.explode(F.col('details'))).drop('details')

# Read location and name
dfReadSpecificStructure ="Exp_RESULTS.user.location.*","*")

Output —

3. Read from HTTP link:

Input JSON file (online data) — let’s read all the users name from the file


Code to read-

To read online JSON we will use urllib.request, open the URL content using utf-8 decoding. After that convert into an RDD and finally create a Dataframe. Same like above will Explode the array and then read the struct ‘name’. Remember if there is no Array element then no need to explode.

from pyspark.sql import SparkSession
from pyspark import SparkContext
from pyspark.sql import functions as F
from urllib.request import Request, urlopen

sc = SparkContext(master="local[*]", appName= "readJSON")
spark = SparkSession.builder.config("spark.sql.warehouse.dir", "file:///C:/temp").appName("readJSON").getOrCreate()

# Online data source
onlineData = ''

# read the online data file
httpData = urlopen(onlineData).read().decode('utf-8')

# convert into RDD
rdd = sc.parallelize([httpData])

# create a Dataframe
jsonDF =

# read all the users name:
readUser = jsonDF.withColumn('Exp_Results',F.explode('results')).select('*')

Output —

4. CSV file contains JSON column:

Input CSV file (mixJSON.csv) — the input file is a CSV where “Info” column contains JSON like data. Will read Student name, Nationality and Subject details.

Code to read-

Will read the CSV file with option(“multiLine”,”true”) to get the multi line JSON format and option(‘escape’,”\””) to ignore inside JSON content (Info column in the above pic). Then we will use JSON_TUPLE to read the required details from the JSON content. JSON_TUPLE has two parameters, 1st is the column name and next are required tag values we are interested in.

from pyspark.sql import SparkSession
from pyspark.sql import functions as F

spark = SparkSession.builder.config("spark.sql.warehouse.dir", "file:///C:/temp").appName("readJSON").getOrCreate()

# escape all " in the JSON content to read properly
readCSVFileDF ="multiLine","true").option('escape',"\"").option('header',True).csv('mixJSON4.csv')

# use JSON_TUPLE to read required contents : student, nationality and subject from Info column
readJSONContentDF1 ="*",F.json_tuple("Info","student","nationality","subject")).drop('Info')

# use JSON_TUPLE to read student details - gender, city, email
readJSONContentDF2 ='*',F.col('c1').alias('Nationality'),F.json_tuple('c0','gender','city','email').alias('Gender','City','MailId')).drop('c0','c1')

# use JSON_TUPLE to read subject details - mainsubject and optional
finalDF ='*',F.json_tuple('c2','main','optional').alias('MainSubject','OptionalSubject')).drop('c2')

# another way : using withcolumn
readJSONDF ="*",F.json_tuple("Info","student","nationality","subject")).drop('Info')

finalDF1 = readJSONDF.withColumn('Gender',F.json_tuple('c0','gender')).\

Output —

# another way : using withcolumn

Happy Reading !!



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store