You are currently viewing Convert JSON Object to String in Python

We are often required to convert JSON objects to a String in Python, you could do this in multiple ways, for example, json.dumps() is utilized to convert JSON to string and also we can directly use the str() and repr() methods that will convert json object to a string.

Advertisements

Following are the methods to convert JSON object to String;

  1. Converting json to string by using json.dumps()
  2. Converting json data from file to string by using json.dumps() with json.load()
  3. Converting json to string by using str()
  4. Converting json to string by using repr()

1. Quick Examples of Converting JSON Object to String

Following are quick examples to understand converting json object to a string. In the later sections, I will describe these in detail.


# Quick examples of converting JSON object to string
import json

# Method 1 : Json to string - Using json.dumps()
country_string = json.dumps(country_json)
print(type(country_string))

# Method 2 : Read json data from file and convert to string
with open('country1.json') as country_json1:
  country_dict = json.load(country_json1)
  country_string = json.dumps(country_dict) 
  print(type(country_string))

# Method 3 : Using str()
print(str(country_json))

# Method 4 : Using repr()
print(repr(country_json))

2. Using json.dumps() to Convert JSON Object to String

The json.dumps() method is available in the json module which allows you to convert a python object into a json string. It converts into json string, If we try to return the type of the converted object, it is of type ‘str’.

2.1 Syntax

Following is the syntax of the json.dumps() method.


# Syntax
json.dumps(python_object)

Here, python_object is like a dictionary, a list of dictionaries etc.

2.2 Examples

Example 1: Let’s have a python dictionary with country details and convert this into json string.


import json

# Dictionary object representing as JSON
country_json = { "Country Name": "US","States": ["California", "Texas","Ohio"],"Lakes_Available":"Yes"}

# Json to string - Using json.dumps()
country_string = json.dumps(country_json)
print(country_string)
print(type(country_string))

# Output:
# {"Country Name": "US", "States": ["California", "Texas", "Ohio"], "Lakes_Available": "Yes"}
# 

We can see that after applying json.dumps(), the object (Dictionary) is converted into string.

Example 2: Let’s have a json file and convert this data into a string.

Here, we will use the json.load() method to load the json into our environment. After that we will use the same like previous example to convert into string.


import json

# Json file to string
with open('country1.json') as country_json1:
  country_dict = json.load(country_json1)
  country_string = json.dumps(country_dict) 
  print(country_string)
  print(type(country_string))

# Output:
# {"Country Name": "US", "States": ["California", "Texas", "Ohio"], "Lakes_Available": "Yes"}
# 

3. Using str() to Convert JSON object to String

The str() convert the given python object to string. It takes an object as a parameter and converts it to a string. If you pass JSON object, it converts the object to a string.

3.1 Syntax

Let’s see how to use the str() method.


str(json_object)

3.2 Example

Let’s have json data with country details and convert this into json string using str().


import json

country_json = { "Country Name": "US","States": ["California", "Texas","Ohio"],"Lakes_Available":"Yes"}

# Using str()
print(str(country_json))

# Output:
# {'Country Name': 'US', 'States': ['California', 'Texas', 'Ohio'], 'Lakes_Available': 'Yes'}

4. Using repr()

The repr() is also used to represent the python object in string format. Like str(), it won’t convert the given JSON object to a string. It just shows the string representation of the given JSON object, Like str() it will take json_object as a parameter.

4.1 Syntax

Following is an example of the repr() method.


# Syntax of repr()
repr(json_object)

4.2 Example

By using the repr() built-in function, let’s convert the json data with country details into a json string.


# Import json module
import json

# Dictionary objectinternally represented as JSON
country_json = { "Country Name": "US","States": ["California", "Texas","Ohio"],"Lakes_Available":"Yes"}

# Using repr()
print(repr(country_json))

# Output:
# {'Country Name': 'US', 'States': ['California', 'Texas', 'Ohio'], 'Lakes_Available': 'Yes'}

5. Conclusion

You have seen how to convert the json object to a string in python using the json.dumps(), str(), and repr(). By using json.dumps() approach, we have seen converting JSON object to a string and json file into a string through json.load() method.