How to read JSON file in Python

Muthu
how-to-program
Published in
Dec 29, 2021

Objective

To read the JSON file and save it to the variable

import jsonclass JSONReader:
def __init__(self, file_path: str):
self.file_path = file_path
def read(self):
with open(self.book_path, 'r') as myfile:
data = myfile.read()
return json.loads(data)
if __name__ == "__main__":
reader = JSONReader("/tmp/a.json")
print(reader.read())

The file context manager can read the file as string, then json.loads(<string>) will convert the string value to the JSON object

--

--

Muthu
Muthu

Written by Muthu

» 9+ years of experience in Data engineering, Dashboard designing » 3+ years of experience in Web application development

No responses yet