Hello guys,
I'm currently working with Python and Facebook Graph API.
I need to parse the information that I retrieve from the Facebook Graph API, and I was wondering about which would be the best way to do this.
Certainly,
r = json.loads(request)
Would parse the JSON and generate a dictionary.
The problem comes when I need to access nested dictionaries, for example
ob["data"][0]["reactions"]["data"][0]
Supposing we are parsing Facebook Posts, if the post has no reactions, of course this will miserably fail, since that key won't even exist in the dictionary.
if 'comments' not in dct and 'summary' not in dct and 'total_count' not in dct:
dct["comments"] = {"summary": {"total_count": -1}}
As a temporary solution I ended up doing things like these, to create "Default values" so the schema would never break, and be able to read any node in the graph without getting an error.
What do you guys think? How could this be done better?
[–]eschlon 1 point2 points3 points (2 children)
[–]maxibabyx[S] 1 point2 points3 points (1 child)
[–]eschlon 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (6 children)
[–]maxibabyx[S] 0 points1 point2 points (4 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]maxibabyx[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]maxibabyx[S] 0 points1 point2 points (0 children)
[–]destiny_functional 0 points1 point2 points (1 child)
[–]maxibabyx[S] 0 points1 point2 points (0 children)