In this Python tutorial we will see how to convert a JSON object to string. The builtin module json exposes a method dumps() to convert JSON object to string. Let us take a look at the below example.

Example – Python Convert JSON To String

In this example, we are converting the JSON object in the variable “json_obj” by using the function dumps(). We are also printing the type of the converted variable which is string.

import json
json_obj = {
  "employees": [
    {
      "name": "John Watson",
      "dateOfJoin": "01/01/2018",
      "active": True,
      "awards": None
    },
    {
      "name": "William Ben",
      "dateOfJoin": "01/01/2015",
      "active": False,
      "awards": 1
    }
  ]
}
#Convert object to string.
json_str = json.dumps(json_obj)
print(json_str)
print(type(json_str))

Output:

{"employees": [{"name": "John Watson", "dateOfJoin": "01/01/2018", "active": true, "awards": null}, {"name": "William Ben", "dateOfJoin": "01/01/2015", "active": false, "awards": 1}]}
<class 'str'>

Note: Take a look at the boolen values, its converted from “True” to “true”. Similarly “None” is converted to “null”. Look at the post on conversion of JSON to string to know more about the type conversion details.

Python Convert JSON To String
Tagged on: