Open In App

Convert JSON to string – Python

Last Updated : 08 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Data is transmitted across platforms using API calls. Data is mostly retrieved in JSON format. We can convert the obtained JSON data into String data for the ease of storing and working with it. Python provides built-in support for working with JSON through the json module. We can convert JSON data into a string using the json.dumps() method.

Let’s see how to convert JSON to String.

Json to String on dummy data using “json.dumps”

This code creates a Python dictionary and converts it into a JSON string using json.dumps(). The result is printed along with its type, confirming that the output is now a string.

Python
import json

# create a sample json
a = {"name" : "GeeksforGeeks", "Topic" : "Json to String", "Method": 1}

y = json.dumps(a)

print(y)
print(type(y))

Output: 

jsonToString

Explanation:

  • json.dumps(a) converts the dictionary a into a JSON-formatted string.
  • print(y) prints the JSON string.
  • print(type(y)) prints the type of the variable y, which will be <class ‘str’> since the JSON data is now a string.

Json to String using an API using requests and “json.dumps”

This code makes a GET request to a dummy API to fetch employee data, converts the JSON response into a Python dictionary using json.loads(), and then converts it back into a JSON string using json.dumps(). The string is printed along with its type.

Python
import json 
import requests 

# Get dummy data using an API
res = requests.get("http://dummy.restapiexample.com/api/v1/employees")

# Convert data to dict
d = json.loads(res.text)

# Convert dict to string
d = json.dumps(d)

print(d)
print(type(d))

Output: 

Explanation:

  • requests.get() fetches data from the API.
  • json.loads(res.text) converts the JSON string to a dictionary.
  • json.dumps(d) converts the dictionary back to a JSON string.

Next Article
Practice Tags :

Similar Reads