0

I have a JSON file where I am parsing the data, below is what I have tried and did not workout.

response = requests.post('https://satest.com', headers=headers, data=data)
response1=response.json()
print(response1[0].evtName)
for value in response1:
        if value['evtName'] = re.match(r'\b(\w*TY-AB12\w*)\b', "TY-AB12"): #I want to loop in json Exactly match the evtName with TY-AB12
              print(response1[value]['evtName'])

Objective : to loop through the json and output all the value of evtName , evt and PG which has TY-AB12 associated in the evtname (TY-AB12 sample app2 url)

Can anyone help here on how to proceed here.

2 Answers 2

2

Simple soution

If you want all items, that have their evtName starting withTY-AB12 in their evtName just look for basic string inclusion

values = response1["data"]
for value in values:
    if value['evtName'].startswith('TY-AB12'):
        print(value)

{'evt': 85, 'evtName': 'TY-AB12 sample app1 url', 'Config': {'CG': 'TTFG', 'PG': '0002'}}
{'evt': 86, 'evtName': 'TY-AB12 sample app2 url', 'Config': {'CG': 'TTFG', 'PG': '0000'}}

Alternatives :

  • evtName contains the pattern : if 'TY-AB12' in value['evtName']:

  • evtName matches a regex : if re.match('TY-AB12', value['evtName']): (fullmatch also exists)


Given a response1 that is

response1 = {
    "name": "testappsa",
    "data": [{"evt": 85, "evtName": "TY-AB12 sample app1 url", "Config": {"CG": "TTFG", "PG": "0002"}},
             {"evt": 85, "evtName": "TY-AB34 sample app1 url", "Config": {"CG": "TTKL", "PG": "0006"}},
             {"evt": 86, "evtName": "TY-AB12 sample app2 url", "Config": {"CG": "TTFG", "PG": "0000"}},
             {"evt": 88, "evtName": "TY ALL - TY-AB12ALL app3 url", "Config": {"CG": "TFFY", "PG": "000"}}]
}
Sign up to request clarification or add additional context in comments.

3 Comments

No I dont have a dict element thats why I did response1=response.json()
my response looks something like this jsfiddle.net/xgo9n243/2 How should I iterate for this ??
@Thomas you may understand what the dict is, you just need to using "data" key to get the values look my edit
0

You can loop over all the events and check if the evtName contains TY-AB12. if it does then just print the values you are interested in.

import json
with open('test.dat') as test_data:
    json_data = json.load(test_data)
    for event in json_data:
        if 'TY-AB12' in event['evtName']:
            print(event['evt'], event['evtName'], event['Config']['PG'])

OUTPUT

85 TY-AB12 sample app1 url 0002
86 TY-AB12 sample app2 url 0000
88 TY ALL - TY-AB12ALL app3 url 000

6 Comments

I cannot use start with because if you notice the JSON there is another evtName which should be in the expected output TY ALL - TY-AB12ALL app3 url so will regex be a better option here ? I guess
In that case just use in
if I do json_data = json.load(test_data) It gives error as return loads(fp.read(), AttributeError: 'dict' object has no attribute 'read'
This is in illustration, where i am loading up the json data. You already have the json_data so you would just do for event in response1: since you already have the json object. you can just ignore the lines before the for loop.
okay is this step necessary ? response1=response.json() because if I directly work with response1 as you said it gives me error TypeError: string indices must be integers
|

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.