how-to-program
Published in

how-to-program

How to read JSON file in Python

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

--

--

--

Recommended from Medium

AGDK tutorial step by step

Breaking changes: creating big changes.

Gitlab Pipeline With AWS Codedeploy

Alt Text

28 Cloud Cost Saving Actions for AWS & Azure | Nutanix Beam

Moving forward with Kotlin #2: Inheritance

NetSuite: View Recent Logins

Design Principles — SOLID from Clean Architecture

Niagara Pro’s Swipe Actions

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
<muTheTechie/>

<muTheTechie/>

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

More from Medium

Python String Repetition

Python list, tuple, and set.

LEARNING PYTHON -Datetime

If Statements in Python