Loop through the list of skills
print("\nSkills:") for skill in data['skills']: print("-", skill) ```
How JSON Types Map to Python Types
When you use json.load(), the data is converted as follows:
| JSON | Python |
|---|---|
| object | dict |
| array | list |
| string | str |
| number (int) | int |
| number (real) | float |
true |
True |
false |
False |
null |
None |
Common Issues and Solutions
-
FileNotFoundError: This happens if Python can't find your file.- Solution: Make sure the file name is spelled correctly and the file is in the same directory as your script. If it's in a different directory, provide the full or relative path (e.g.,
'data/my_file.json').
- Solution: Make sure the file name is spelled correctly and the file is in the same directory as your script. If it's in a different directory, provide the full or relative path (e.g.,
-
json.JSONDecodeError: This means your file is not valid JSON.- Solution: Check your
.jsonfile for syntax errors. The most common mistakes are:- Using single quotes (
') instead of double quotes ("). - Having a trailing comma after the last item in an object or list.
- Missing commas between elements.
- Using single quotes (
- Solution: Check your
-
Character Encoding Issues: If your file contains special characters (like
éorü), you might get aUnicodeDecodeError.- Solution: Specify the encoding when opening the file. UTF-8 is the most common and is standard for JSON.
python with open('data.json', 'r', encoding='utf-8') as file: data = json.load(file)
- Solution: Specify the encoding when opening the file. UTF-8 is the most common and is standard for JSON.
Reading a JSON String vs. a JSON File
It's important to know the difference between json.load() and json.loads().
json.load(): Reads from a file object.json.loads(): Reads from a string. (The 's' stands for "string").
Example using json.loads():
```python import json
json_string = """ { "name": "Brenda", "city": "New York" } """
