Google News
logo
Convert a Python object containing all the legal data types
Here's an example of converting a Python object containing all the legal data types to a JSON string :
Program :
import json

data = {
    "string": "Hello, world!",
    "integer": 42,
    "float": 3.14159,
    "boolean": True,
    "none": None,
    "list": [1, 2, 3],
    "dict": {"key1": "value1", "key2": "value2"}
}

json_data = json.dumps(data)

print(json_data)
Output :
{"string": "Hello, world!", "integer": 42, "float": 3.14159, "boolean": true, "none": null, "list": [1, 2, 3], "dict": {"key1": "value1", "key2": "value2"}}
In the above example, we first create a Python object called `data` that contains different data types such as string, integer, float, boolean, None, list, and dictionary.

We then use the `json.dumps()` function to convert this Python object to a JSON string, and store it in the `json_data` variable. Finally, we print the JSON string using the `print()` function.