We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Sending JSON

JSON isn't just something we get from the server, we can also send JSON data!

In Python, we use the json module which provides two main methods: json.loads() and json.dumps().

json.dumps()

json.dumps() is particularly useful for sending JSON.

As you may expect the JSON dumps() function does the opposite of loads. It takes a Python dictionary or list as input and converts it into a string. This is useful when we need to serialize the objects into strings to send them to our server or store them in a database.

import json

data: dict[str, str | bool] = {"name": "waseem", "chad": True}
json_string = json.dumps(data)
print(json_string)  # {"name": "waseem", "chad": true}
# only the boolean changed!

Assignment

Users need to be able to mark their projects as completed. However, update_project_by_id(project_id: str, project_obj: Project) -> Project is passing an invalid value to pyfetch, causing an error.

The body parameter in pyfetch expects a string when sending JSON data. You need to convert the Python dictionary to a JSON string before sending it.

Fix the code by converting the project_obj dictionary to a JSON string using json.dumps().