nmacc nmacc - 6 days ago 5
JSON Question

Python 2 iterate over nested json dictionary

I have a dict sourced from documents in Elasticsearch with JSON that looks like this:

{
u'hits':{
u'hits':[
{
u'_score':1.0,
u'_type':u'input',
u'_id':u'1',
u'_source':{
u'message':u'I really love fake news tho.'
},
u'_index':u'gossip'
}
],
u'total':1,
u'max_score':1.0
},
u'_shards':{
u'successful':1,
u'failed':0,
u'total':1
},
u'took':3,
u'timed_out':False
}


There are several thousand documents and I am looking to get a list of each of the 'message' values. I have tried several approaches but cannot seem to get a functioning solution. My current attempt is:

messages = es.search(index="gossip", _source=['message'])
for key, value in messages.iteritems():
print key, value


which results in:

hits{
u'hits':[
{
u'_score':1.0,
u'_type':u'input',
u'_id':u'1',
u'_source':{
u'message':u'I really love fake news tho.'
},
u'_index':u'gossip'
}
],
u'total':1,
u'max_score':1.0
}_shards{
u'successful':1,
u'failed':0,
u'total':1
}


Hoping somebody can nudge me in the right direction.

Answer

Try this -

es_res = {  
   u'hits':{  
      u'hits':[  
         {  
            u'_score':1.0,
            u'_type':u'input',
            u'_id':u'1',
            u'_source':{  
               u'message':u'I really love fake news tho.'
            },
            u'_index':u'gossip'
         }
      ],
      u'total':1,
      u'max_score':1.0
   },
   u'_shards':{  
      u'successful':1,
      u'failed':0,
      u'total':1
   },
   u'took':3,
   u'timed_out':False
}

print [ i['_source']['message'] for i in es_res['hits']['hits']]
Comments