Joseph Drane
Joseph Drane

Reputation: 103

Search JSON response with python

I have searched and searched but I'm not able to figure this out. Below is the JSON data I get back from the web service, API:

{
  "total_count": 673, 
  "items": [
    {
      "hap": "delivered", 
      "message": "Delivered: [email protected] \u2192 [email protected] 'Some Email subject'", 
      "type": "info", 
      "created_at": "Wed, 19 Aug 2015 18:38:54 GMT", 
      "message_id": "[email protected]"
    }, 
    {
      "hap": "accepted", 
      "message": "Accepted: [email protected] \u2192 [email protected] 'Subject of this email here'", 
      "type": "info", 
      "created_at": "Wed, 19 Aug 2015 18:38:53 GMT", 
      "message_id": "[email protected]"
    }, 
    {
      "hap": "delivered", 
      "message": "Delivered: [email protected] \u2192 [email protected] 'Subject Line here'", 
      "type": "info", 
      "created_at": "Wed, 19 Aug 2015 18:37:50 GMT", 
      "message_id": "[email protected]"
    },

The challenge is that I am trying to search the "message": block for the TO email address which comes after the "\u2192" inside the "message": location.

I have created this python script that dumps all entries inside "message": but I have not been able to filter this with a specific email address.

import requests, json
print("Connecting to the URL...")
r = requests.get("https://api:[email protected]/v3/example.com/log")
j = r.json()
for data in j['items']:
    print data['message']

Upvotes: 0

Views: 2236

Answers (3)

Dan
Dan

Reputation: 1126

Either of these "should" work.

Since you already know the email address, you only need to search the string for the exact email addresss. There are a couple of options here. You can use regular expressions (maybe overkill since this isnt a pattern but a known string). You can also just search the string for the known email address.

You determine if you should use the message based of its boolean value in both cases.

Option 1

Regular Expressions

https://docs.python.org/3.5/library/re.html#match-objects

import re

email_address = "[email protected]"

for data in j['items']:
    match = re.search(email_address, data['message'])
    if match:
        print data['message']

Option 2

Search the message for the email address string

email_address = "[email protected]"

for data in j['items']:
    if email_address in data['message']:
        print data['message']

Upvotes: 1

winterfall
winterfall

Reputation: 164

Use the json.loads to solve that.

>>> json.loads('"Delivered: [email protected] \u2192 [email protected]"')
'Delivered: [email protected][email protected]'

Upvotes: 0

Assem
Assem

Reputation: 12097

Try:

re.findall("[^@ ]+@[^@]+\.[^@ ]+", data['message'].split("\u2192")[1])[0]

First I split data['message'] on two by the character \u2192 than take the second part. I try to find all emails in this second part, and choose only the first one because it is what you are looking for.

Upvotes: 0

Related Questions