Sai Krishna
Sai Krishna

Reputation: 8187

How to convert this particular json string into a python dictionary?

How do I convert this string ->

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

into a python dictionary like so ->

data = {
         "name" : "sam"
       }

Upvotes: 4

Views: 10202

Answers (2)

zuanfan
zuanfan

Reputation: 62

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

now the data is the dict

Upvotes: 1

NPE
NPE

Reputation: 500357

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.

Upvotes: 15

Related Questions