user2268507
user2268507

Reputation:

List to JSON Object

I am being returned data in this form:

['{\n  "data": {\n    "promoter": {\n      "instagram_id": 123,\n      "instagram_name": "user",\n      "instagram_token": "\\"123\\"",\n      "location": "sydney",\n      "profile_pic": "profile/pic.jpg",\n      "user_id": 1,\n      "username": "my_user_name"\n    },\n    "success": true\n  }\n}']

I am confused as to how I can convert this into a JSON object such that I can do:

json_obj['data']['promoter']['instagram_id']

I have tried converting the list into a JSON String with dumps and then using loads but to no avail.

Any help with this would be appreciated.

Upvotes: 1

Views: 49

Answers (1)

Régis B.
Régis B.

Reputation: 10588

You should convert only the first element of the list, with json.loads. Demo:

>>> data = ['{\n  "data": {\n    "promoter": {\n      "instagram_id": 123,\n      "instagram_name": "user",\n      "instagram_token": "\\"123\\"",\n      "location": "sydney",\n      "profile_pic": "profile/pic.jpg",\n      "user_id": 1,\n      "username": "my_user_name"\n    },\n    "success": true\n  }\n}']
>>> import json
>>> json_obj = json.loads(data[0])
>>> json_obj['data']['promoter']['instagram_id']
123

Upvotes: 3

Related Questions