Introduction
JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used for configuration files and API communication.
While shell tools can process plain text well, structured formats such as JSON are usually easier and safer to manipulate in Python using built-in libraries.
In this lab, you will act as a developer who needs to update an application's configuration. You will create a JSON configuration file, write a Python script to load it, modify a setting, and save the changes to a new file.
Create Sample Data
In this step, you will create a sample JSON configuration file. This file simulates a typical settings file for a software application, containing keys for the application name, version, debug mode, and connection limits.
First, ensure you are in the correct working directory:
JSON is one of the most common structured data formats in modern operations because APIs, cloud tools, and many configuration systems all use it. Learning to read and change it safely is much more valuable than treating it as plain text.
cd ~/project
Now, create a file named config.json using a text editor like nano.
nano config.json
Paste the following content into the file. This JSON object contains a boolean value (false), an integer (100), and strings.
{
"appName": "SuperApp",
"version": "1.0.0",
"debug": false,
"maxConnections": 100
}
To save and exit nano:
To save and exit nano, press Ctrl + O to write the changes, followed by Enter to confirm the filename. Then, press Ctrl + X to exit the editor.
You can verify the file content using the cat command:
cat config.json
You should see the JSON content printed to the terminal.
Read JSON
In this step, you will write a Python script to read the config.json file. Python's json module converts JSON data into a Python dictionary.
This is where Python has a clear advantage over basic shell scripting. Instead of searching text with regular expressions and hoping the structure stays simple, Python can parse the file according to JSON rules and give you real data types to work with.
Think of json.load() as "unpacking" a shipping box. It takes the text stored in the file (the box) and converts it into a Python dictionary (the contents) that you can easily use and modify in your code.
Create a new Python script named parse_json.py:
nano parse_json.py
Add the following code to the file. This script imports the json module, opens the configuration file, and loads the data.
import json
## Open the JSON file
with open('config.json', 'r') as file:
## Load the data into a Python dictionary
data = json.load(file)
## Print the data to verify it was loaded correctly
print("Current Configuration:")
print(data)
print(f"Type of data: {type(data)}")
Save and exit nano (Ctrl+O, Enter, Ctrl+X).
Now, run the script to see the output:
python3 parse_json.py
You should see the dictionary printed to the console, confirming that Python has successfully parsed the JSON file.
Modify Data
In this step, you will modify the loaded data within your Python script. Since json.load() converts the data into a standard Python dictionary, you can modify values just like you would with any other dictionary.
Notice the type translation happening here. JSON true and false become Python True and False. Numbers stay numeric instead of turning into raw strings. This makes later comparisons and calculations much safer.
Open parse_json.py again to edit it:
nano parse_json.py
Modify the script to change the value of the debug key from False (which corresponds to false in JSON) to True. Add the modification logic after loading the data but before printing it.
Update your code to look like this:
import json
with open('config.json', 'r') as file:
data = json.load(file)
print("Original Debug Status:", data['debug'])
## Modify the value
data['debug'] = True
print("Updated Debug Status:", data['debug'])
Save and exit nano.
Run the script again to verify the change in memory:
python3 parse_json.py
You should see the output showing the original status as False and the updated status as True. Note that this change currently only exists in the Python program's memory and has not been saved to a file yet.
Save Changes
In this step, you will save the modified dictionary to a new JSON file. This is done using the json.dump() function.
This read-modify-write cycle is a very common automation pattern. The script loads structured data, changes only the intended fields, and writes valid JSON back out. That is far more reliable than line-based editing when the file structure grows more complex.
Think of json.dump() as "packing" the box. It takes your Python dictionary (the items) and converts it back into text format (the sealed box) so it can be stored in a file. It is good practice to save to a new file (e.g., new_config.json) to avoid overwriting the original configuration while testing.
Open parse_json.py one last time:
nano parse_json.py
Add the code to write the data dictionary to a file named new_config.json. We will use the indent=4 parameter to make the output file human-readable (pretty-printed).
Your final script should look like this:
import json
## 1. Read the original file
with open('config.json', 'r') as file:
data = json.load(file)
## 2. Modify the data
data['debug'] = True
## 3. Write to a new file
with open('new_config.json', 'w') as file:
json.dump(data, file, indent=4)
print("Configuration updated and saved to new_config.json")
Save and exit nano.
Run the script to perform the read-modify-write operation:
python3 parse_json.py
If successful, the script will print the confirmation message.
Verify Result
In this step, you will verify that the new configuration file was created and contains the updated value.
Verification is especially important with structured files because a script can succeed syntactically while still producing the wrong logical content. Reading the output back is how you confirm both file creation and correctness of the data change.
List the files in your directory to confirm new_config.json exists:
ls -l
Now, display the content of the new file:
cat new_config.json
You should see output similar to this:
{
"appName": "SuperApp",
"version": "1.0.0",
"debug": true,
"maxConnections": 100
}
Notice that "debug" is now true.
You can also compare the original and new files using the diff command to see exactly what changed:
diff config.json new_config.json
The output will highlight the difference between the debug lines.
Summary
In this lab, you have successfully learned how to handle JSON data using Python. You started by creating a raw JSON configuration file, then wrote a Python script to parse that file into a dictionary. You modified the data programmatically and saved the updated structure back to a new JSON file.
This workflow—Read, Modify, Write—is a fundamental pattern in automation and DevOps scripting. While Bash is great for running commands, Python's ability to handle structured data like JSON makes it a powerful tool for managing complex configurations and API data.



