I have a json file as below:
[
{
"contributors": null,
"coordinates": null,
"created_at": "Fri Aug 04 21:12:59 +0000 2017",
"entities": {
"hashtags": [
{
"indices": [
32,
39
],
"text": "\ubd80\uc0b0\ucd9c\uc7a5\uc548\ub9c8"
},
{
"indices": [
40,
48
],
"text": "\ubd80\uc0b0\ucd9c\uc7a5\ub9c8\uc0ac\uc9c0"
}
]
},
"text": "\uaedb"
"retweeted_status": {
"contributors": null,
"coordinates": null,
"created_at": "Fri Aug 04 20:30:06 +0000 2017",
"display_text_range": [
0,
0
],
"text": "hjhfbsdjsdbjsd"
},
"extended_tweet": {
"display_text_range": [
0,
137
],
"entities": {
"hashtags": [
{
"indices": [
62,
75
],
"text": "2ndAmendment"
},
{
"indices": [
91,
104
],
"text": "1stAmendment"
}
]
}
}
}
]
I wrote the below python code to count the number of text attributes throughout the json file.
data = json.load(data_file)
for key, value in data1.items():
if key=="text":
cnt+=1
elif key=="retweeted_status":
for k,v in value.items():
if k=="text":
cnt+=1
elif key == "entities":
if key.keys()=="hashtags" :
for k1,v1 in key:
# Difficult to loop further
Since the data structure doesn't remain constant it becomes difficult to iterate. Further I want to access the value of the text attribute and display it. Is there any simpler way to do this without multiple loops?
if key.keys()=="hashtags"will never beTrue, btwif 'text' in key: cnt += 1if 'text' in data.get('retweeted_status', {}): cnt += 1, etc. No loop necessary.textfromextended_tweetvalues?