Pages

11 November, 2023

Access a Json inside a string?

Suppose I have a single dictionary/Json in Python which is inside a List:


Minimum Working Example:
response = [{ "UserStor": "id1","StoryTitle": "title1","StoryState": "state1", "UserStoryType": "type1","RawUpdates": "updates1","RawComments": "comments1"}]



If I need to access this dictionary, I can do:
print(response[0])

#output
{'UserStor': 'id1', 'StoryTitle': 'title1', 'StoryState': 'state1', 'UserStoryType': 'type1', 'RawUpdates': 'updates1', 'RawComments': 'comments1'}


---



Now, I have another dictionary but, it is inside a string
response = "{'UserStor': 'id1', 'StoryTitle': 'title1', 'StoryState': 'state1', 'UserStoryType': 'type1', 'RawUpdates': 'updates1', 'RawComments': 'comments1'}"



How, can I access this dictionary?


I know I can use ast.literal_eval().


Is there any other way to access this dictionary without using literal_eval()/eval() ?


I am here to learn, your comments and answers are welcome.

No comments:

Post a Comment

Thanks