I'm trying to scrape various user data from a JSON API, and then store those values in a MySQL database I've set up, like so:
response = requests.get("URL") json_obj = json.loads(response.text) timer = json_obj['timestamp'] jobposition = json_obj['job']['position'] query = "INSERT INTO users (timer, jobposition) VALUES (%s, %s)" values = (timer, jobposition) cursor = db.cursor() cursor.execute(query, values) db.commit() The code seems to run fine for the most part, but some users don't have the attributes I'm trying to scrape in the JSON, which leads to errors.
How can I just store a zero value, or other default value, in the database when data is missing from the JSON?