Read JSON File Using Python Code and Prompt

May 30, 2018

2 mins read

Published in

Read JSON File using Python requires importing the JSON library of Python. Before we deep dive in code let me brief you about the JSON.

JSON (Javascript Object Notation) is easy to read, write, analyze and flexible string-based format which can be used to serialized (store) and transfer between multiple application, products and between the multiple layers (Client/Server).

I have been playing around with JSON from the last 5 years in Python, Javascript, NodeJS, and JAVA.

Program to Read JSON file using Python:

1
2
3
4
5
6
import json

with open('jsonfile.txt') as jsonfile:
    parsed = json.load(jsonfile)

print json.dumps(parsed, indent=2, sort_keys=True)

Create a jsonfile.txt using this JSON data. a parsed variable will become a dictionary object out of parsing JSON file. Please check in the image, how the parsed object looks like in debug mode. to understand more about “json.dump”, please visit JSON Pretty Print using Python.