Wednesday, July 3, 2024
HomeLanguagesPythonPython – Convert JSON to string

Python – Convert JSON to string

Data in 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.

Let’s see how to convert JSON to String.

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

Python3




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


Output: 
 

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

Python3




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))


Output: 

 

Dominic Rubhabha Wardslaus
Dominic Rubhabha Wardslaushttps://neveropen.dev
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments