Skip to content

Convert Python string to JSON | Example code

  • by

Use json.loads() method to convert JSON string to JSON in Python. This method will convert JSON string into dict.

json.loads(string)

Python string to JSON example

Simple example code.

Import json module then uses the loadS() method. This method accepts a valid json string and returns a dictionary.

import json

# JSON string
sj = '{ "name":"John", "age":30}'
print(type(sj))

res = json.loads(sj)

print(res)
print(type(res))

Output:

Python string to JSON

Another example

Can access all elements.

import json

s = '{"success": "true", "status": 200, "message": "Hello"}'

d = json.loads(s)

print("Response", d["status"])

Output: Response 200

Do comment if you have any doubts or suggestions on this Python JSON topic.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading