Python Curriculum
Json Overview
When you write scripts to automate infrastructure, you will constantly need to send data between different systems. For example, you might query a cloud provider API to get a list of servers, or write configuration settings for a web application. The industry-standard format for sharing this data is JSON (JavaScript Object Notation).
In this guide, we will learn how JSON works. We will compare JSON and Python data structures, understand formatting rules, and write a simple script to parse JSON text.
What is JSON?
JSON is a lightweight, text-based format for storing and exchanging data. It is easy for humans to read and write, and easy for computers to parse and generate. JSON is completely language-independent, meaning you can generate JSON in Python and read it in Go, Java, or JavaScript without any translation tools.
Let's compare JSON data types to their Python equivalents.
Here is a diagram showing how JSON data maps to Python data structures when parsed:
Basic JSON Rules
JSON has strict syntax rules that you must follow, or else parsers will fail to read your data:
- Double Quotes Only: All string values and key names must be wrapped in double quotes (
"). Single quotes (') are invalid in JSON. - Boolean Cases: Booleans must be written in lowercase:
trueorfalse. - No Trailing Commas: You cannot leave a comma after the last item in an object or array.
- Null Value: Missing values must be written as
nullin lowercase.
Create a file named json_basics.py and write the code below.
Run this file in your terminal.
Expected output:
Real-World Example: Server Profile Metadata
Let's look at a structured JSON payload representing a server profile. We will parse this metadata, inspect values, and verify if the node matches our compliance guidelines.
Create a file named audit_json.py and write the code below.
Run the script.
Expected output:
Common Mistakes
Using Single Quotes in JSON Strings
A common mistake is writing key names or values using single quotes. While this is valid in Python, it is invalid in JSON and will crash the parser.
- The Bad Way:
- The Good Way:
Leaving Trailing Commas
If you leave a trailing comma after the last key-value pair or array item, JSON parser tools will throw a decode error.
- The Bad Way:
- The Good Way:
Capitalizing Booleans or Null Values
In JSON, booleans and null placeholders must be written in lowercase. Writing True, False, or None (which are valid in Python) will cause parsing failures.
- The Bad Way:
- The Good Way:
Summary
- JSON (JavaScript Object Notation) is a lightweight, text-based data format used by web APIs and configs.
- JSON is language-independent, allowing different programs to exchange data.
- JSON objects map directly to Python dictionaries, and JSON arrays map to Python lists.
- All string values and keys in JSON must be wrapped in double quotes (
"). - JSON booleans (
true,false) and nulls (null) must be lowercase. - Trailing commas are strictly forbidden in JSON arrays and objects.
- Use
json.loads(text)to convert a JSON string into a Python dictionary. - The
JSONDecodeErrorexception is raised when parsing invalid JSON text. - Catching syntax issues prevents scripts from crashing when reading corrupt API responses.
- Parsing JSON configurations dynamically helps automate infrastructure audits.