Convert Python string to JSON | Example code

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 use 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 and suggestions on this Python JSON topic.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

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


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.