Updated April 3, 2023
Introduction to Python JSON to string
In this article, we will see how to convert JSON format to string using Python programming language. But before that, we should know what is JSON; this is elaborated as text written in javascript object notation. In Python, to deal with such JSON formats or files, we have to import json module in the Python program, which is a built-in module. Moreover, the data is usually transmitted using API calls to retrieve JSON data; therefore, we need to convert it into a string for easy storing and working. In this article, we will see a method provided by json module in Python for converting JSON to string such as dumps().
Working on Converting JSON to string in Python with Examples
This section will see how to convert JSON format to a normal string format in Python. Firstly, to convert any JSON format to string in the Python program, we need to import json module in the program. There are two ways of converting JSON to string in Python; one way is to use json method dumps(), and another way is to use an API such as requests module in Python.
In the below section, let us demonstrate these two ways with examples.
To convert JSON format file to string in Python using json module method dumps(). Let us see how to convert it in a string format.
Example #1
Code:
import json
print("Program to demonstrate conversion of JSON to strig in Python:")
print("\n")
course = {"course_name" : "Python", "Institute_name" : "EDUCBA", "course_place": "Delhi", "course_fees" : "25000"}
print("The JSON format is as follows:")
print(course)
print("\n")
print("The type of the given data is as follows")
print(type(course))
print("\n")
print("The above JSON format is converted to following string")
con_str = json.dumps(course)
print(con_str)
print("\n")
print("The type of converted data is as follows")
print(type(con_str))
Output:
In the above program, we have first imported json module, and then we will declare a variable “course” in which we will store JSON data, and the type of variable course is printed using the type( course ) method, which will result in type as <dict> and then we will use dumps() method to store the JSON data in string format which means Python objects have to be stored in a file in string format. So in the above program, we will pass this JSON data to the dumps( course ) method, and the result of this method is in string format, which is stored in a variable “con_str”. So we can see the type of the “con_str” variable, which will result in string format <str>.
Now we will see another way of converting JSON data to string in Python using API using requests module. In this, we get data using API, which is JSON data to convert it to a string.
Example #2
Code:
import json
import requests
print("Program to demonstrate conversion of JSON to string using requests module")
print("\n")
print("The dummy json data by https://api.twitter.com/1.1/statuses/user_timeline.json?count=10&screen_name=Rbloggers")
r = requests.get("https://api.twitter.com/1.1/statuses/user_timeline.json?count=10&screen_name=Rbloggers")
json_data = json.loads(r.text)
print(json_data)
print("\n")
print(type(json_data))
print("\n")
print("The converted data from JSON to string is as follows:")
con_data = json.dumps(json_data)
print(con_data)
print("\n")
print(type(con_data))
Output:
In the above program, we first import json and requests modules in the Python program. Here we import json module to deal with JSON data, and we import the requests module to fetch the JSON data directly from the website using the get() method of the requests module. So in this program, we fetch JSON data using requests.get() method by passing the proper address in this; we have to take dummy JSON data from Twitter. Then we will store this data in the variable “json_data” using the loads() function, and to know what is the type of this variable, we have passed this variable to function type(). Then we will convert this JSON data to a string using the dumps() function, and we pass the variable “json_data”, which contains JSON data, and the converted string format is stored in the variable “con_data”. Therefore, we can see the result of converted string and the type of the variable “con_data”, which will result in <str>. So the output of the above program is shown in the above screenshot.
From the above examples, we can also convert the back string to JSON format. This is just the opposite of what we did in the above section. This is also very simple to convert string to JSON data where we first import json module and then we use a loads() function which accepts a valid string and returns valid JSON data. We should note that we can convert JSON format into any Python object in Python, such as string, list, dict, etc. Usually, JSON data will be of the list or dictionary format. Now let us see JSON data converted to floating-point output with a small example.
Example #3
Code:
import json
from decimal import Decimal
print("The JSON data is as follows")
json_data = '{"number": 4.6789394}'
print(json_data)
print("\n")
print("The converted JSON data to float type data is as follows:")
float_data = json.loads(json_data, parse_float = Decimal)
print(float_data['number'])
Output:
We can see the output screenshot in the above program, where we have JSON data into float data.
Conclusion
In this article, we can conclude that converting JSON data to string format in the Python programming language is simple. In this article, we use json module to deal with JSON data, and we also use the dumps() function, which takes JSON data and converts it into the string format. We also can fetch JSON data directly through the website using the requests module in Python with get() function.
Recommended Articles
This is a guide to Python JSON to string. Here we discuss the introduction and working on converting JSON to string in python with examples. You may also have a look at the following articles to learn more –