4

How do I convert this string ->

 string = [{"name":"sam"}]

into a python dictionary like so ->

data = {
         "name" : "sam"
       }

2 Answers 2

15
In [1]: import json

In [2]: json.loads('[{"name":"sam"}]')
Out[2]: [{u'name': u'sam'}]

This returns a list, the first element of which is the desired dictionary.

Sign up to request clarification or add additional context in comments.

Comments

1
string = [{ "name" : "sam" }]
data = string[0]

now the data is the dict

1 Comment

Further, even if you had a string that contained a valid Python expression, you'd have to eval it to get the corresponding object, which raises a number of security issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.