Recommended Free Tools
If posting["salary"] raises KeyError, the mapping you are reading does not contain a "salary" key. For an optional field, use posting.get("salary", "Not listed") or another fallback that fits your code. For a required field, keep the missing value visible and report it instead of silently inventing one.
Why does reading a job posting raise KeyError?
Python raises KeyError when bracket lookup asks a mapping for a key it does not have. The exception’s key identifies what lookup failed; it does not tell you why the key is absent. The posting may use a different spelling or capitalization, the value may be nested elsewhere, or the input may not have the shape your code expects. Python’s built-in types documentation describes mapping operations and their behavior.
As an Amazon Associate I earn from qualifying purchases.
A job posting decoded from JSON may be represented as a dictionary, but there is no single field schema guaranteed by Python. Inspect a representative decoded record and its keys before assuming where a value lives or that a field is always present.
How to read an optional field without an exception
Use get() when the key is optional and your code can sensibly proceed without it:
#1 Best Overall
salary = posting.get("salary", "Not listed")
This returns the fallback if the key is absent and does not add that key to a normal dictionary. Choose the fallback for its intended use: None, an empty list, and a display string are different values and may lead to different behavior downstream.
How to handle required fields
For a field your application requires, a fallback can hide malformed or incomplete input. Keep the failure explicit and turn it into an informative validation error:
Rank #2
try:
title = posting["title"]
except KeyError as exc:
raise ValueError("Job posting is missing required field 'title'") from exc
Whether a title, identifier, or other field is required depends on your application’s contract, not on Python or a universal job-posting format. Handle an invalid record according to that contract—for example, by reporting or quarantining it.
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsHow to distinguish an absent key from a null value
posting.get("salary") returns None both when the key is absent and when it exists with the value None. If that distinction matters, use a unique sentinel:
_MISSING = object()
salary = posting.get("salary", _MISSING)
if salary is _MISSING:
print("salary key is absent")
elif salary is None:
print("salary key exists but has a null value")
You can also test membership before looking up the value:
if "salary" in posting:
salary = posting["salary"]
else:
salary = "Not listed"
This makes the presence check explicit and preserves the difference between an absent key and a present key whose value is None.
When to use get(), brackets, setdefault(), or defaultdict
| Situation | Pattern | Behavior and trade-off |
|---|---|---|
| Optional field | mapping.get(key, fallback) |
Returns a chosen fallback for a missing key without mutating a normal dictionary. |
| Required field | mapping[key] with explicit error handling |
Keeps missing required input visible so your application can explain the problem. |
| Presence itself matters | if key in mapping, then access |
Separates an absent key from a present key whose value is None. |
| Initialize an entry while grouping | setdefault() or defaultdict |
Both support accumulation; each can mutate the mapping when creating a missing entry. |
Use setdefault() when mutation is intended
setdefault(key, default) returns the existing value or inserts and returns the supplied default when the key is missing. It is useful when you intend to add an entry as part of building grouped data. The Python collections documentation shows it as an alternative in a grouping example.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchPC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Use defaultdict for repeated accumulation
A defaultdict calls its default_factory when bracket access requests a missing key, then inserts the generated value. For example, defaultdict(list) can collect records under a grouping key, while defaultdict(int) can count occurrences.
Best Value
from collections import defaultdict
by_company = defaultdict(list)
by_company[company].append(posting)
That behavior applies to bracket access, not get(). The Python 3.14.8 collections documentation states that get() behaves like normal dictionary lookup and returns None by default rather than calling the factory. Use the collection only when automatic insertion is wanted; it is not a substitute for validating required fields.
Check the record shape when the key should be present
If a lookup fails unexpectedly, inspect both the object and the level where you are looking. A key may be spelled differently, have different capitalization, or belong to a nested mapping. Calling get() on one mapping does not make nested lookups safe:
company = posting.get("company")
if isinstance(company, dict):
name = company.get("name")
else:
name = None
Validate each layer against the input your application accepts. Before treating a JSON-decoded value as a dictionary, check its type and shape; a decoded JSON value is not necessarily a mapping.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




