Python – Convert JSON to string

Data in transmitted accross platforms using API calls. Data is mostly retrived in JSON format. We can convert the obtained JSON data into String data for the ease of storing and working with it.

Let’s see how to convert JSON to String.

Method #1: Json to String on dummy data using “json.dumps”

filter_none

edit
close

play_arrow

link
brightness_4
code

import json
  
# create a sample json
  
a = {"name" : "GeeksforGeeks", "Topic" : "Json to String", "Method": 1}
  
# Convert JSON to String
  
y = json.dumps(a)
  
print(y)
print(type(y))

chevron_right


Output:



Method #2: Json to String using an API using requests and “json.dumps”

filter_none

edit
close

play_arrow

link
brightness_4
code

import json 
import requests 
  
# Get dummy data using an API
  
# Convert data to dict
data = json.loads(res.text)
  
# Convert dict to string
data = json.dumps(data)
  
print(data)
print(type(data))

chevron_right


Output:

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.