Read JSON Files Using Python

Parse JSON Files

JavaScript Object Notation or JSON is an open data interchange language standard.

It is a text format that is programming language-independent.

JSON is a collection of key/value pairs. The JSON object begins with a left curly brace and ends with a right curly brace. Each key/value pair is separated by a colon.

JSON Object

Collection

JSON Collections
Name Description Example
Array Ordered arrangement of values “names”: [“Jane”, “Jake”, “John”]
String Sequence of alphanumeric text or symbols “name”: “John”
Number Integers and floating point numeric types “age”: 24
Whitespace Represents horizontal or vertical space “linefeed”: “\n”
Boolean True or false “completed”: true
Null Represents non-existent of invalid object “extra”: null
JSON Recommend if applicable 2024

import json

# JSON String
jsonstr = '{"name": "John", "age": 24, "graduate": true}'

# Parse JSON String
pythonobj = json.loads(jsonstr)

# Read Values By Key
name = pythonobj['name']
age = pythonobj['age']
graduate = pythonobj['graduate']
combined = f"My name is {name}, and I am {age} years old and it is {graduate} that I am a graduate."
print(combined)

Python JSON Parser Code
Geany Lightweight IDE Displaying Python Code For Parsing JSON String

Compiled Python Code
Geany Lightweight IDE Displaying Compiled Python Code For Parsing JSON String

Executed Python Code
Geany Lightweight IDE Displaying Executed Python Code For Parsing JSON String


Usage

You can use any IDE, text editor or the Python interactive shell to parse JSON data. In this tutorial, a JSON string was used, but the code can easily be expanded to read the contents of a JSON file and parse.

Morality Source

JSON is licensed under the JSON License. This allows commercial use, modification, distribution, and allows making derivatives proprietary. The clause “The Software shall be used for Good, not Evil” is incompatible with other open source licenses.

Learning Python:

Course is optimized for your web browser on any device.

OjamboShop.com Learning Python
OjamboShop.com Learning Python Interactive Course

Limited Time Offer:

OjamboShop.com is offering 20% off coupon code SCHOOL for Learning Python until End Day 2024.

Conclusion:

Python makes it easy to parse JSON data after importing the JSON module. JSON can be parsed as a string or from an external file.

Take this opportunity to learn Python programming language by making a one-time purchase at Learning Python. A web browser is the only thing needed to learn Python in 2024 at your leisure. All the developer tools are provided right in your web browser.

References: