Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert following string to JSON in python

How can i convert the below string to JSON using python?

str1 = "{'a':'1', 'b':'2'}"
like image 764
Murali Avatar asked Apr 23 '26 08:04

Murali


1 Answers

The json library in python has a function loads which enables you to convert a string (in JSON format) into a JSON. Following code for your reference:

import json

str1 = '{"a":"1", "b":"2"}'
data = json.loads(str1)

print(data)

Note: You have to use ' for enclosing the string, whereas " for the objects and its values.

like image 175
Rahul Soni Avatar answered Apr 27 '26 12:04

Rahul Soni



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!