Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
222 views
in Technique[技术] by (71.8m points)

Find specific keyword in API JSON response - Python

I am trying to fetch a JSON response of multiple issues from an API and I am able to get the response successfully. My next part which I want to perform is to fetch/print only those lines which have specific keywords as "moviepass" and "login" in JSON tag "body". Here is my code

import json
import requests

api_url = '***************************************'
headers = {'Content-Type': 'application/json',
           'Authorization':'Basic **************************'}

response = requests.get(api_url, headers=headers)
#print(response.text)

words = ('moviepass', 'login')

def lookingfor(words):
    data = response.text
    for line in data:
        for word in words:
            match = re.findall(word, line['body'])
                if match:
                    print((word, line[]))


lookingfor(words)

My JSON looks like:

[{"tags":["moviepass"],"assignee_name":null,"app_id":"*******","hs_user_id":"*******","title":"1234","redacted":false,"updated_at":1611753805497,"messages":[{"body":"moviepass - Not '
 'sure if this is what you guys meant or not but here '
 'haha.","created_at":********,"author":{"name":"abc","id":"*****","emails":["[email protected]"]},"origin":"end-user","id":"*********"}]
question from:https://stackoverflow.com/questions/65932901/find-specific-keyword-in-api-json-response-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You dont need regular expression.You can use json_data['tags'] But if you want to use regular expression, you need to convert json to string by using

import json
json.dumps(json_obj) #returns same object but type of string.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...