I have a multilevel/complex json file - twitter.json and I want to extract ONLY the author ID from this json file.
This is how my file 'twitter.json' looks:
[
[
{
"tweets_results": [
{
"meta": {
"result_count": 0
}
}
],
"youtube_link": "www.youtube.com/channel/UCl4GlGXR0ED6AUJU1kRhRzQ"
}
],
[
{
"tweets_results": [
{
"data": [
{
"author_id": "125959599",
"created_at": "2021-06-12T15:16:40.000Z",
"id": "1403732993269649410",
"in_reply_to_user_id": "125959599",
"lang": "pt",
"public_metrics": {
"like_count": 0,
"quote_count": 0,
"reply_count": 1,
"retweet_count": 0
},
"source": "Twitter for Android",
"text": "⌨️ Canais do YouTube:\n\n1 - Alexandre Garcia: Canal de Brasília"
},
{
"author_id": "521827796",
"created_at": "2021-06-07T20:23:08.000Z",
"id": "1401998177943834626",
"in_reply_to_user_id": "623794755",
"lang": "und",
"public_metrics": {
"like_count": 0,
"quote_count": 0,
"reply_count": 0,
"retweet_count": 0
},
"source": "TweetDeck",
"text": "@thelittlecouto"
}
],
"meta": {
"newest_id": "1426546114115870722",
"oldest_id": "1367808835403063298",
"result_count": 7
}
}
],
"youtube_link": "www.youtube.com/channel/UCm0yTweyAa0PwEIp0l3N_gA"
}
]
]
I have read through many similar SO questions (including but not limited to):
- Access the key of a multilevel JSON file in python
- Multilevel JSON Dictionary - can't extract key into new dictionary
- How to extract a single value from JSON response?
- how to get fields and values from a specific key of json file in python
- How to select specific key/value of an object in json via python
- Python: Getting all values of a specific key from json
But the structures of those jsons are pretty simple and when I try to replicate that, I hit errors.
From what I read, contents.tweets_results.data.author_id
is how the reference would go. And I am loading using contents = json.load(open("twitter.json"))
. Any help is appreciated.
EDIT: Both @sammywemmy's and @balderman's code worked for me. I accepted @sammywemmy's because I used that code, but I wanted to credit them both in some way.