4

I'm using python-requests to work with an elasticsearch index. In particular I'm not getting what I want from requests.json() (wraps the json.loads() function in the standard lib).

I'm struggling with the result returned by the json() method. It seems to be returning a list of strings. I want a dict object that I can loop through to access each members attributes i.e...

r = requests.get(url)
results = r.json() //same as results = json.loads(json string)
print(results)

for result in results:
    print(result['source'])

This is what elastic search returns when I manually use curl to perform the search (this is the input to the r.json() call):

{"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":18,"max_score":1.0,"hits":[{"_index":"feed","_type":"article","_id":"Bq2_NxWIRZS5mfX_jxv5YQ","_score":1.0, "_so    urce" : {"source": ["http://www.naturalnews.com/040631_farmed_salmon_GMO_sustainable_fishing.html"], "link": ["http://www.naturalnews.com/040631_farmed_salmon_GMO_sustainable_fishing.html"], "description    ": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/Food/Salmon-Raw.jpg\" /></td><td valign=\"top\">Equally as menacing as the potential release of genetically-modified (GM) salmon     into the wild, factory fish farms are a modern scourge responsible for killing off droves of native fish species all around the world. And unless immediate action is taken to address this...</td></tr></t    able>"], "title": ["Fish farming killing off native species; boycott farmed salmon before it's too late"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 193}},{"_index":"feed","_type":    "article","_id":"jxfFD6V-Qwacc4KYNfChcg","_score":1.0, "_source" : {"source": ["http://www.naturalnews.com/040628_calf_exercises_soleus_stretch_lower_body_workouts.html"], "link": ["http://www.naturalnew    s.com/040628_calf_exercises_soleus_stretch_lower_body_workouts.html"], "description": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/Exercise/Running-Shoes-Fitness-Legs-Calves-Mus    cles.jpg\" /></td><td valign=\"top\">Building killer calves is more complicated than people think. Most exercisers do a bunch of standing calf exercises like standing calf raises, sled hack calf raises,     donkey raises, and calf presses to bulk up not realizing that their entire workout is only targeting...</td></tr></table>"], "title": ["Calf exercises: Sit down to build yourself up"], "published": ["Wed    , 5 Jun 2013 00:00:00 CST"], "medoozie_score": 228}},{"_index":"feed","_type":"article","_id":"RBV1P_lXStKAHeaNi-q9Bg","_score":1.0, "_source" : {"source": ["http://www.naturalnews.com/040626_Hopewood_ch    ildren_natural_living_organic_foods.html"], "link": ["http://www.naturalnews.com/040626_Hopewood_children_natural_living_organic_foods.html"], "description": ["<table><tr><td><img src=\"http://www.natura    lnews.com/gallery/300X250/Children/Kids-Children-Play-Outside-Happy-Exercise.jpg\" /></td><td valign=\"top\">In 1942, the founder of the Natural Health Society of Australia, Leslie Owen Bailey, began a s    ocial experiment: He chose to adopt 85 children whose parents were unable to provide for them. The children's residence became the sprawling Hopewood House in New South Wales...</td></tr></table>"], "tit    le": ["Honoring nature: The lesson of the Hopewood children"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 187}},{"_index":"feed","_type":"article","_id":"uP6G2_leSl2-gUMyUie3eA","_s    core":1.0, "_source" : {"source": ["http://www.naturalnews.com/040636_IRS_scandal_conservative_groups_Obama_Administration.html"], "link": ["http://www.naturalnews.com/040636_IRS_scandal_conservative_gro    ups_Obama_Administration.html"], "description": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/Money/IRS-Building-Revenue-Taxes-Money.jpg\" /></td><td valign=\"top\">Officials wit    h the Internal Revenue Service (IRS) have admitted that the agency inappropriately targeted conservative groups seeking legal tax-exemption status in the years and months leading up to the 2012 election.    &#13;&#10;&#13;&#10;Those same officials have apologized for the targeting...</td></tr></table>"], "title": ["Claim: IRS still targeting conservative groups, even after promising the intimidation already     stopped"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 342}},{"_index":"feed","_type":"article","_id":"yhcqfmKQRwSVhR1vUIy0vg","_score":1.0, "_source" : {"source": ["http://www.natu    ralnews.com/040633_Bug_Eating_Festival_insects_survival_foods.html"], "link": ["http://www.naturalnews.com/040633_Bug_Eating_Festival_insects_survival_foods.html"], "description": ["<table><tr><td><img s    rc=\"http://www.naturalnews.com/gallery/300X250/Women/Party-Girls-Fun.jpg\" /></td><td valign=\"top\">Are people in Austin so hungry they are eating bugs?&#13;&#10;&#13;&#10;\"Well, it does help if you c    ome to the party having skipped lunch,\" says Marjory Wildcraft, the host and creator 6th Annual Bug Eating Festival which will be held on Saturday June 29, 2013 at Zilker Park in Austin...</td></tr></ta    ble>"], "title": ["6th Annual Bug Eating Festival"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 252}},{"_index":"feed","_type":"article","_id":"Grh9KUDVQvSs7oRUjzOG9A","_score":1.0,     "_source" : {"source": ["http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html"], "link": ["http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html"], "description"    : ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/Food/Brown-Eggs-On-White.jpg\" /></td><td valign=\"top\">Considering that eggs contain all the nutrients needed to develop healthy     chicks, it is unsurprising that studies into their numerous health benefits for humans abound. Eating more eggs has, for instance, been repeatedly linked to reduced LDL cholesterol, a lowered risk...</t    d></tr></table>"], "title": ["Peptide found in egg whites can lower blood pressure, say researchers"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 262}},{"_index":"feed","_type":"art    icle","_id":"6OfIM7G9Rke-TD3N6bI4Ow","_score":1.0, "_source" : {"source": ["http://www.naturalnews.com/040629_obesity_vitamin_D_deficiency_exercise.html"], "link": ["http://www.naturalnews.com/040629_obe    sity_vitamin_D_deficiency_exercise.html"], "description": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/Men/Big-Fat-Hairy-Obese-Belly.jpg\" /></td><td valign=\"top\">Confirming w    hat health professionals have long suspected, a new study published in the journal PLOS Medicine provides some of the strongest evidence yet that obesity is indeed a cause of vitamin D deficiency.&#13;&#    10;&#13;&#10;The large study was a collaborative effort between U.S...</td></tr></table>"], "title": ["Obesity causes vitamin-D deficiency: Research"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "med    oozie_score": 313}},{"_index":"feed","_type":"article","_id":"osvu1IlkSwa9yL566YoQuQ","_score":1.0, "_source" : {"source": ["http://www.naturalnews.com/040627_Judge_Napolitano_Eric_Holder_freedom_of_the_    press.html"], "link": ["http://www.naturalnews.com/040627_Judge_Napolitano_Eric_Holder_freedom_of_the_press.html"], "description": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300X250/C    oncepts/Extra-Liar-Newspaper-Headline.jpg\" /></td><td valign=\"top\">Terrorism, and the threat of new attacks, dominated U.S. domestic policy and national security strategy throughout the Bush Administr    ation, but my, how times have changed: Despite the recent terrorist bombings of the Boston Marathon April 19, President Barack Obama appears...</td></tr></table>"], "title": ["Judge Napolitano: Obama mor    e threatened by journalism than terrorism"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 34}},{"_index":"feed","_type":"article","_id":"0XfVTXImTWGwQ1Ko13Q9hg","_score":1.0, "_source    " : {"source": ["http://www.naturalnews.com/040637_mental_illness_depression_women.html"], "link": ["http://www.naturalnews.com/040637_mental_illness_depression_women.html"], "description": ["<table><tr>    <td><img src=\"http://www.naturalnews.com/gallery/300X250/Cancer/Poor-Women-Breast-Cancer.jpg\" /></td><td valign=\"top\">For reasons that are still largely unclear, women are much more likely than men t    o develop some form of mental illness during their lives, whether it be depression, schizophrenia, or simple anxiety, according to a new study. Based on an analysis of 12 large-scale epidemiological...</    td></tr></table>"], "title": ["Women are 40 percent more likely to develop mental illness than men, study concludes"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 434}},{"_index":"fe    ed","_type":"article","_id":"8L699AbUTtmlrnlSJlmzhw","_score":1.0, "_source" : {"source": ["http://www.naturalnews.com/040624_cancer_industry_documentary_lies.html"], "link": ["http://www.naturalnews.com    /040624_cancer_industry_documentary_lies.html"], "description": ["<table><tr><td><img src=\"http://www.naturalnews.com/gallery/300x250/Programs/Cut-Poison-Burn.jpg\" /></td><td valign=\"top\">The stunnin    g, jaw-dropping and tearfully devastating true story of the conventional cancer industry is unleashed in a new documentary called \"Cut, Poison, Burn.\" While celebrities like Michael Douglas are grabbin    g headlines by promoting the dangerous myths and false...</td></tr></table>"], "title": ["Profiteering cancer industry ripped wide open by explosive documentary 'Cut, Poison, Burn' - watch preview here"]    , "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 187}}]}}

Here is what I get when I write the result returned by request.json() to a txt file:

'{"source": ["http://www.naturalnews.com/040640_front_groups_food_industry_processed.html"], "link": ["http://www.naturalnews.com/040640_front_groups_food_industry_processed.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Logos/Pepsi-Logo.jpg\\" /></td><td valign=\\"top\\">Some say it\'s the best public relations that money can buy, because when you can\'t refute the truth about an issue, the next-best thing to do is stack the deck in your favor.&#13;&#10;&#13;&#10;The third edition of a report released last month by the International Food Information Council...</td></tr></table>"], "title": ["Food industry front groups exposed in new report: Monsanto, Coke, Pepsi and more"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 350}'
'{"source": ["http://www.naturalnews.com/040639_Obamacare_insurance_premiums_individual_mandate.html"], "link": ["http://www.naturalnews.com/040639_Obamacare_insurance_premiums_individual_mandate.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300x250/celebrities/Barack-Obama-Portrait.jpg\\" /></td><td valign=\\"top\\">The scramble is on to bring the logistical framework for Obamacare into full fruition before its inaugural arrival in 2014. But as legislators and program architects dig down into the nitty-gritty of what the \\"tax\\" will actually entail in practicality, it is becoming...</td></tr></table>"], "title": ["Obamacare will collapse under the weight of rising premiums"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 121}'
'{"source": ["http://www.naturalnews.com/040638_Garcinia_cambogia_weight_loss_obestity.html"], "link": ["http://www.naturalnews.com/040638_Garcinia_cambogia_weight_loss_obestity.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Men/Belly-Fat.jpg\\" /></td><td valign=\\"top\\">With celeb Dr. Oz advocating its use, Garcinia Cambogia (also known as Malabar tamarind, gambooge and brindleberry), has gained notoriety as a fast-acting and cheap fat buster that both suppresses appetite and prevents fat from being made. For generations, this fruit...</td></tr></table>"], "title": ["Does Garcinia cambogia extract really help with weight loss?"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 120}'
'{"source": ["http://www.naturalnews.com/040637_mental_illness_depression_women.html"], "link": ["http://www.naturalnews.com/040637_mental_illness_depression_women.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Cancer/Poor-Women-Breast-Cancer.jpg\\" /></td><td valign=\\"top\\">For reasons that are still largely unclear, women are much more likely than men to develop some form of mental illness during their lives, whether it be depression, schizophrenia, or simple anxiety, according to a new study. Based on an analysis of 12 large-scale epidemiological...</td></tr></table>"], "title": ["Women are 40 percent more likely to develop mental illness than men, study concludes"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 468}'
'{"source": ["http://www.naturalnews.com/040636_IRS_scandal_conservative_groups_Obama_Administration.html"], "link": ["http://www.naturalnews.com/040636_IRS_scandal_conservative_groups_Obama_Administration.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Money/IRS-Building-Revenue-Taxes-Money.jpg\\" /></td><td valign=\\"top\\">Officials with the Internal Revenue Service (IRS) have admitted that the agency inappropriately targeted conservative groups seeking legal tax-exemption status in the years and months leading up to the 2012 election.&#13;&#10;&#13;&#10;Those same officials have apologized for the targeting...</td></tr></table>"], "title": ["Claim: IRS still targeting conservative groups, even after promising the intimidation already stopped"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 12}'
'{"source": ["http://www.naturalnews.com/040635_cooling_foods_summer_eating_whole.html"], "link": ["http://www.naturalnews.com/040635_cooling_foods_summer_eating_whole.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Women/Woman-Summersault-Grass-Fitness-Play.jpg\\" /></td><td valign=\\"top\\">As we enter into the dog days of summer, it\'s easy to keep cool with specific foods and herbs under our belt. Using the wisdom of Traditional Chinese Medicine (TCM), we can remain balanced, calm and energetic as the mercury rises. Particular suggestions border on simple...</td></tr></table>"], "title": ["Stay cool this summer with natural medicine and herbal remedies"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 495}'
'{"source": ["http://www.naturalnews.com/040634_mental_health_President_Obama_Big_Pharma.html"], "link": ["http://www.naturalnews.com/040634_mental_health_President_Obama_Big_Pharma.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Concepts/Man-Hat-Brain-Concept.jpg\\" /></td><td valign=\\"top\\">President Obama gave psychiatrists and pharmaceutical companies the keys to the kingdom during his keynote speech at the White House Conference on Mental Health this week.&#13;&#10;&#13;&#10;Celebrated as a hero to the shamed and downtrodden, Obama played the part well as he subtly...</td></tr></table>"], "title": ["Obama keynote speech deals death blow to mental health, delivers millions into the hands of big pharma"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 280}'
'{"source": ["http://www.naturalnews.com/040633_Bug_Eating_Festival_insects_survival_foods.html"], "link": ["http://www.naturalnews.com/040633_Bug_Eating_Festival_insects_survival_foods.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Women/Party-Girls-Fun.jpg\\" /></td><td valign=\\"top\\">Are people in Austin so hungry they are eating bugs?&#13;&#10;&#13;&#10;\\"Well, it does help if you come to the party having skipped lunch,\\" says Marjory Wildcraft, the host and creator 6th Annual Bug Eating Festival which will be held on Saturday June 29, 2013 at Zilker Park in Austin...</td></tr></table>"], "title": ["6th Annual Bug Eating Festival"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 191}'
'{"source": ["http://www.naturalnews.com/040632_C-sections_obesity_childbirth.html"], "link": ["http://www.naturalnews.com/040632_C-sections_obesity_childbirth.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Medical/Woman-Giving-Birth-Delivering-Baby-Nurses-Doctors-Hospital.jpg\\" /></td><td valign=\\"top\\">New research out of the U.K. sheds light on yet another possible cause of widespread childhood obesity. Based on an analysis involving more than 10,000 British babies, roughly 9 percent of whom were born via C-section, researchers found that babies delivered the alternate...</td></tr></table>"], "title": ["C-sections lead to obese kids, study finds"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 64}'
'{"source": ["http://www.naturalnews.com/040631_farmed_salmon_GMO_sustainable_fishing.html"], "link": ["http://www.naturalnews.com/040631_farmed_salmon_GMO_sustainable_fishing.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Food/Salmon-Raw.jpg\\" /></td><td valign=\\"top\\">Equally as menacing as the potential release of genetically-modified (GM) salmon into the wild, factory fish farms are a modern scourge responsible for killing off droves of native fish species all around the world. And unless immediate action is taken to address this...</td></tr></table>"], "title": ["Fish farming killing off native species; boycott farmed salmon before it\'s too late"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 281}'
'{"source": ["http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html"], "link": ["http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Food/Brown-Eggs-On-White.jpg\\" /></td><td valign=\\"top\\">Considering that eggs contain all the nutrients needed to develop healthy chicks, it is unsurprising that studies into their numerous health benefits for humans abound. Eating more eggs has, for instance, been repeatedly linked to reduced LDL cholesterol, a lowered risk...</td></tr></table>"], "title": ["Peptide found in egg whites can lower blood pressure, say researchers"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 118}'
'{"source": ["http://www.naturalnews.com/040629_obesity_vitamin_D_deficiency_exercise.html"], "link": ["http://www.naturalnews.com/040629_obesity_vitamin_D_deficiency_exercise.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Men/Big-Fat-Hairy-Obese-Belly.jpg\\" /></td><td valign=\\"top\\">Confirming what health professionals have long suspected, a new study published in the journal PLOS Medicine provides some of the strongest evidence yet that obesity is indeed a cause of vitamin D deficiency.&#13;&#10;&#13;&#10;The large study was a collaborative effort between U.S...</td></tr></table>"], "title": ["Obesity causes vitamin-D deficiency: Research"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 395}'
'{"source": ["http://www.naturalnews.com/040628_calf_exercises_soleus_stretch_lower_body_workouts.html"], "link": ["http://www.naturalnews.com/040628_calf_exercises_soleus_stretch_lower_body_workouts.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Exercise/Running-Shoes-Fitness-Legs-Calves-Muscles.jpg\\" /></td><td valign=\\"top\\">Building killer calves is more complicated than people think. Most exercisers do a bunch of standing calf exercises like standing calf raises, sled hack calf raises, donkey raises, and calf presses to bulk up not realizing that their entire workout is only targeting...</td></tr></table>"], "title": ["Calf exercises: Sit down to build yourself up"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 469}'
'{"source": ["http://www.naturalnews.com/040627_Judge_Napolitano_Eric_Holder_freedom_of_the_press.html"], "link": ["http://www.naturalnews.com/040627_Judge_Napolitano_Eric_Holder_freedom_of_the_press.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Concepts/Extra-Liar-Newspaper-Headline.jpg\\" /></td><td valign=\\"top\\">Terrorism, and the threat of new attacks, dominated U.S. domestic policy and national security strategy throughout the Bush Administration, but my, how times have changed: Despite the recent terrorist bombings of the Boston Marathon April 19, President Barack Obama appears...</td></tr></table>"], "title": ["Judge Napolitano: Obama more threatened by journalism than terrorism"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 96}'
'{"source": ["http://www.naturalnews.com/040626_Hopewood_children_natural_living_organic_foods.html"], "link": ["http://www.naturalnews.com/040626_Hopewood_children_natural_living_organic_foods.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Children/Kids-Children-Play-Outside-Happy-Exercise.jpg\\" /></td><td valign=\\"top\\">In 1942, the founder of the Natural Health Society of Australia, Leslie Owen Bailey, began a social experiment: He chose to adopt 85 children whose parents were unable to provide for them. The children\'s residence became the sprawling Hopewood House in New South Wales...</td></tr></table>"], "title": ["Honoring nature: The lesson of the Hopewood children"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 445}'
'{"source": ["http://www.naturalnews.com/040625_lawsuit_Monsanto_genetic_pollution.html"], "link": ["http://www.naturalnews.com/040625_lawsuit_Monsanto_genetic_pollution.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Farming/Man-Farmer-Wheat-Crop-Field-Clouds-Sky.jpg\\" /></td><td valign=\\"top\\">The next wave of farmer backlash against Monsanto has just been unleashed by Ernest Barnes, a wheat farmer in Morton County, Kansas. He filed suit this week in the U.S. District Court in Wichita, Kansas, alleging that Monsanto\'s genetic pollution has financially damaged...</td></tr></table>"], "title": ["It\'s on! Farmers begin suing Monsanto over genetic pollution of wheat crops"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 311}'
'{"source": ["http://www.naturalnews.com/040624_cancer_industry_documentary_lies.html"], "link": ["http://www.naturalnews.com/040624_cancer_industry_documentary_lies.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300x250/Programs/Cut-Poison-Burn.jpg\\" /></td><td valign=\\"top\\">The stunning, jaw-dropping and tearfully devastating true story of the conventional cancer industry is unleashed in a new documentary called \\"Cut, Poison, Burn.\\" While celebrities like Michael Douglas are grabbing headlines by promoting the dangerous myths and false...</td></tr></table>"], "title": ["Profiteering cancer industry ripped wide open by explosive documentary \'Cut, Poison, Burn\' - watch preview here"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 216}'
'{"source": ["http://www.naturalnews.com/040623_Morninglane_Dairy_Vernon_Hershberger_food_freedom.html"], "link": ["http://www.naturalnews.com/040623_Morninglane_Dairy_Vernon_Hershberger_food_freedom.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Farming/Farmer-Crops.jpg\\" /></td><td valign=\\"top\\">There is a war being waged against real food - no, not the heavily-processed, chemical-laden garbage that fills the aisles of most major supermarkets today, but actual wholesome food grown on clean, family-scale farms across the U.S. And the upcoming documentary Let...</td></tr></table>"], "title": ["\'Let Them Eat Grass\' documentary to expose massive government tyranny against small-scale farmers producing real food"], "published": ["Tue, 4 Jun 2013 00:00:00 CST"], "medoozie_score": 211}'
{u'hits': {u'hits': [{u'_score': 0.32461247, u'_type': u'article', u'_id': u'yBqM5NSoTDCAeXo2YRO1hQ', u'_source': {u'description': [u'<table><tr><td><img src="http://www.naturalnews.com/gallery/300X250/Concepts/Man-Hat-Brain-Concept.jpg" /></td><td valign="top">President Obama gave psychiatrists and pharmaceutical companies the keys to the kingdom during his keynote speech at the White House Conference on Mental Health this week.&#13;&#10;&#13;&#10;Celebrated as a hero to the shamed and downtrodden, Obama played the part well as he subtly...</td></tr></table>'], u'title': [u'Obama keynote speech deals death blow to mental health, delivers millions into the hands of big pharma'], u'source': [u'http://www.naturalnews.com/040634_mental_health_President_Obama_Big_Pharma.html'], u'link': [u'http://www.naturalnews.com/040634_mental_health_President_Obama_Big_Pharma.html'], u'medoozie_score': 93, u'published': [u'Wed, 5 Jun 2013 00:00:00 CST']}, u'_index': u'feed'}, {u'_score': 0.16524655, u'_type': u'article', u'_id': u'Grh9KUDVQvSs7oRUjzOG9A', u'_source': {u'description': [u'<table><tr><td><img src="http://www.naturalnews.com/gallery/300X250/Food/Brown-Eggs-On-White.jpg" /></td><td valign="top">Considering that eggs contain all the nutrients needed to develop healthy chicks, it is unsurprising that studies into their numerous health benefits for humans abound. Eating more eggs has, for instance, been repeatedly linked to reduced LDL cholesterol, a lowered risk...</td></tr></table>'], u'title': [u'Peptide found in egg whites can lower blood pressure, say researchers'], u'source': [u'http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html'], u'link': [u'http://www.naturalnews.com/040630_egg_whites_blood_pressure_peptides.html'], u'medoozie_score': 262, u'published': [u'Wed, 5 Jun 2013 00:00:00 CST']}, u'_index': u'feed'}, {u'_score': 0.16524655, u'_type': u'article', u'_id': u'6OfIM7G9Rke-TD3N6bI4Ow', u'_source': {u'description': [u'<table><tr><td><img src="http://www.naturalnews.com/gallery/300X250/Men/Big-Fat-Hairy-Obese-Belly.jpg" /></td><td valign="top">Confirming what health professionals have long suspected, a new study published in the journal PLOS Medicine provides some of the strongest evidence yet that obesity is indeed a cause of vitamin D deficiency.&#13;&#10;&#13;&#10;The large study was a collaborative effort between U.S...</td></tr></table>'], u'title': [u'Obesity causes vitamin-D deficiency: Research'], u'source': [u'http://www.naturalnews.com/040629_obesity_vitamin_D_deficiency_exercise.html'], u'link': [u'http://www.naturalnews.com/040629_obesity_vitamin_D_deficiency_exercise.html'], u'medoozie_score': 313, u'published': [u'Wed, 5 Jun 2013 00:00:00 CST']}, u'_index': u'feed'}, {u'_score': 0.15372275, u'_type': u'article', u'_id': u'RBV1P_lXStKAHeaNi-q9Bg', u'_source': {u'description': [u'<table><tr><td><img src="http://www.naturalnews.com/gallery/300X250/Children/Kids-Children-Play-Outside-Happy-Exercise.jpg" /></td><td valign="top">In 1942, the founder of the Natural Health Society of Australia, Leslie Owen Bailey, began a social experiment: He chose to adopt 85 children whose parents were unable to provide for them. The children\'s residence became the sprawling Hopewood House in New South Wales...</td></tr></table>'], u'title': [u'Honoring nature: The lesson of the Hopewood children'], u'source': [u'http://www.naturalnews.com/040626_Hopewood_children_natural_living_organic_foods.html'], u'link': [u'http://www.naturalnews.com/040626_Hopewood_children_natural_living_organic_foods.html'], u'medoozie_score': 187, u'published': [u'Wed, 5 Jun 2013 00:00:00 CST']}, u'_index': u'feed'}], u'total': 4, u'max_score': 0.32461247}, u'_shards': {u'successful': 5, u'failed': 0, u'total': 5}, u'took': 66, u'timed_out': False}

How can I get json.loads(json-string) to return a dictionary instead of a string or list of strings? If this is not possible then what would be a good way for me to turn each string in the list into dictionary?

Turns out the r.json() function was never broken. I just didn't know how to loop through the results.

Here is how I modified my code so that I could loop through it...

r = requests.get('http://localhost:9200/feed/_search?q=' + query)
results = r.json()

for result in results['hits']['hits']:
    print(result['_source']['title'])

Here is a smaller sample of the json I'm decoding...

http://pastebin.com/rpCDyL6F

guyja
  • 857
  • 1
  • 10
  • 19
  • Your question is a bit messy and the test sets are kinda large. How about constructing a minimal not-working example? – kirelagin Jun 05 '13 at 13:54
  • The text file you provided looks like a dictionary. Are you saying that this is held in a string value? – SuperFamousGuy Jun 05 '13 at 13:55
  • @SuperFamousGuy Indeed the second file contains a string. Note the double-quotes. Python interpreter uses them only inside strings. – kirelagin Jun 05 '13 at 13:56
  • @kirelagin I'm not certain how to make a smaller example. Its only large because the search is returning ~ 20 results. – guyja Jun 05 '13 at 14:16
  • @SuperFamousGuy I guess you were right. In trying to create a simpler example I realized that I could loop through the result set returned by the r.json() function. Initially, I was trying to access the items as I looped over the results. I kept getting an error telling me that it was a string. I saw the "'" in the file and assumed that I was getting a giant string. Long story short, nothing was ever wrong with r.json(). – guyja Jun 05 '13 at 15:09
  • @kirelagin added a smaller sample of the json results. Thank you. – guyja Jun 05 '13 at 15:19
  • @user2455857 So, As I can see, `results` is a Python dictionary. `results['hits']['hits']` is a Python list of Python dictionaries (each representing an article). What is your question? – kirelagin Jun 05 '13 at 15:21
  • @user2455857 Glad to hear it! That's much cleaner than trying to eval the stuff returned. What kind of crazy person would suggest that anyway? :) – SuperFamousGuy Jun 05 '13 at 15:34

2 Answers2

3

Turns out the r.json() function was never broken. I just didn't know how to loop through the results.

Here is how I modified my code so that I could loop through it...

r = requests.get('http://localhost:9200/feed/_search?q=' + query)
results = r.json()

for result in results['hits']['hits']:
    print(result['_source']['title'])

Here is a smaller sample of the json I'm decoding...

http://pastebin.com/rpCDyL6F

guyja
  • 857
  • 1
  • 10
  • 19
-1

Unfortunately I can't say I'm too familiar with the standard json.loads function and what it provides, so please note this is probably not the best way to go about this. That said, beyond JSON parsing generally the best way to convert a string to a python object is with ast.literal_eval. There's a good article on it's use, and dangers found here. There's also a good post about eval vs ast.literal_eval and how they differ here

Keep in mind that in order to eval a string into a python object it must first be python syntax. Also, as this is coming from a JSON call be warned, this is not good practice as JSON does not always translate to Python. Specifically true, false, and null are not valid values in Python, and will cause an error to be thrown. On top of that if you're not 100% confident in utilizing what you're receiving as objects there's room for some serious injection security flaws here.

All that said if you're at a dead end and feeling desperate:

import ast
x = ast.literal_eval('{"source": ["http://www.naturalnews.com/040640_front_groups_food_industry_processed.html"], "link": ["http://www.naturalnews.com/040640_front_groups_food_industry_processed.html"], "description": ["<table><tr><td><img src=\\"http://www.naturalnews.com/gallery/300X250/Logos/Pepsi-Logo.jpg\\" /></td><td valign=\\"top\\">Some say it\'s the best public relations that money can buy, because when you can\'t refute the truth about an issue, the next-best thing to do is stack the deck in your favor.&#13;&#10;&#13;&#10;The third edition of a report released last month by the International Food Information Council...</td></tr></table>"], "title": ["Food industry front groups exposed in new report: Monsanto, Coke, Pepsi and more"], "published": ["Wed, 5 Jun 2013 00:00:00 CST"], "medoozie_score": 350}')

works on my machine.

Community
  • 1
  • 1
SuperFamousGuy
  • 1,455
  • 11
  • 16
  • I've tried eval(r.json()) which doesn't make sense to me. I also don't think eval(r) will solve the problem because r is an object. I'm not too familiar with eval() but it seems more suited towards executing strings containing python commands such as eval("2+2"). I'm trying to get json.loads(data) to create a dictionary that I can loop through. – guyja Jun 05 '13 at 14:24
  • Right, eval evaluates a string into an object. Your json string is already a dictionary, just needs to be evaluated into a python object. I typed the wrong variable up there, editing now. – SuperFamousGuy Jun 05 '13 at 14:59
  • -1 `eval` is not a substitute for a JSON parser. Not only is there already a perfectly good JSON parser, JSON isn't compatible with Python syntax (for starters, `true` and `false` and `null` aren't valid Python), `eval` is insecure and overly permissive, etc. –  Jun 05 '13 at 14:59