0

I'm trying to get certain strings from a website but having difficulty. This is the array (blocking out certain values)

{
    "ts": 1468156734285,
    "inbox": "email",
    "created": 302,
    "expire_in": 86098,
    "last_eml": 1468156432,
    "emls": [{
        "eml": "this is the email id",
        "eml_hash": "email hash",
        "eml_destroy_hash": "3319b480",
        "subject": "Welcome to your new temporary inbox",
        "from_address": "[email protected]",
        "from_name": "welcome",
        "status": "read",
        "received": 302,
        "size": 0
     }]
}

In the 'emls' array, I'm trying to get 'eml' and 'eml_hash' as a python string. This is my code so far:

response = requests.get(url)
data = response.json()
print data
emal = data['emls']
eml_hash = data['eml_hash'] 

But it can't find these values

Traceback (most recent call last):
File "test.py", line 40, in <module>
emal= data['eml']

KeyError: 'eml' Thanks

3
  • What is the output of print data? And are you this is the code you are running? in your code emal = data['emls'] but in the error emal = data['eml'] Commented Jul 10, 2016 at 13:43
  • {u'created': 0, u'emls': [{u'status': u'unread', u'received': 0, u'from_name': u'welcome', u'from_address': u'[email protected]', u'eml': u'5782524e96af80bf583b57a1', u'eml_destroy_hash': u'65b15f22', u'eml_hash': u'1c759d60', u'size': 0, u'subject': u'Welcome to your new temporary inbox'}], u'last_eml': 1468158543, u'ts': 1468158542729L, u'inbox': u'[email protected]', u'expire_in': 86400} Commented Jul 10, 2016 at 13:48
  • Sorry, had been modifying code. emal = data['eml'] prints the error: Traceback (most recent call last): File "test.py", line 40, in <module> emal= data['eml'] KeyError: 'eml' emls prints: [{u'status': u'unread', u'received': 0, u'from_name': u'welcome', u'from_address': u'[email protected]', u'eml': u'5782531d96af80bf583b57d4', u'eml_destroy_hash': u'4a88e4a2', u'eml_hash': u'82d5112f', u'size': 0, u'subject': u'Welcome to your new temporary inbox'}] Commented Jul 10, 2016 at 13:55

3 Answers 3

1

This code

response = requests.get(url)
data = response.json()
print data
emls = data['emls'] # emls = [{'status': 'unread','received': 0,...}]

Makes emls equal the list with a dictionary inside. So you need to take the first (0th) item of that list. Then you can use the keys eml and eml_hash.

eml = emls[0]['eml']
eml_hash = emls[0]['eml_hash]

If emls has more than one email in, simply loop over it:

eml = [] # List of all eml, email IDs.
eml_hash = [] # list of all eml_has, email hashes

for email in emls:
    eml += [email['eml]]
    eml_hash = [email['eml_hash']]
Sign up to request clarification or add additional context in comments.

Comments

0

This should be pretty straightforward, given that data is the dictionary you provided:

data = {
"ts": 1468156734285,
"inbox": "email",
"created": 302,
"expire_in": 86098,
"last_eml": 1468156432,
"emls": [{
    "eml": "this is the email id",
    "eml_hash": "email hash",
    "eml_destroy_hash": "3319b480",
    "subject": "Welcome to your new temporary inbox",
    "from_address": "[email protected]",
    "from_name": "welcome",
    "status": "read",
    "received": 302,
    "size": 0
}]}

for email in data['emls']:
    print(email['eml'])
    print(email['eml_hash'])

Comments

0

What you are doing is actually indexing the root of the dictionary, data, again. Therefore, you wouldn't get the expected output:

response = requests.get(url)
data = response.json()
print data
emal = data['emls']
# You should not be indexing data again. Note that emls is a list too.
eml_hash = data['eml_hash'] 

So your code would be:

response = requests.get(url)
data = response.json()
print data
eml = data['emls']
eml_hash = data['emls'][0]['eml_hash']

Or:

response = requests.get(url)
data = response.json()
print data
eml = data['emls']
eml_hash = eml[0]['eml_hash']

Outputs:

>>> eml
[{u'status': u'read', u'received': 302, u'from_name': u'welcome', u'from_address': u'[email protected]', u'eml': u'this is the email id', u'eml_destroy_hash': u'3319b480', u'eml_hash': u'email hash', u'size': 0, u'subject': u'Welcome to your new temporary inbox'}]
>>> eml_hash
u'email hash'

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.