0

I have a json data as below and i have one uri to match in each fields. If match found then don't go for next iteration of [fields]. Concept is the users have clicked urls after searching. If user clicked the 3rd url then collect 1st, 2nd, 3rd data values. Dont go for 4th value.

If user clicked 5th url then collect 1st to 5th fields uri and exit that json object. Take a new json object and do same procedure.

[document]-> [fields1] -> [uri]
[document]-> [fields2] -> [uri]
[document]-> [fields3] -> [uri]
..... 
.. till 20-30 times. 

    I have written below code, but the above logic is not working. Kindly help on this. 



 uri='http://abcd.com/123.html'
    print(uri)
    for index_srch_log,row_srch_log in df_search_log_mongo.iterrows():
        RESPONSE = row_srch_log['RESPONSE']
        json_response = json.loads(RESPONSE)
        if 'documents' in json_response:
            field_data=json_response['documents']

            for row_resp_list in field_data:
                print('uri:',row_resp_list['fields']['uri'])
                match_found=False
                for i in row_resp_list['fields']['uri']:
                    print('i',i)

                    if uri == i:
                        print('yes matched')
                        match_found=True
                        break
                        print('found')
                    else:
                        print('not matched')
                        match_found=False
                    if match_found==True:
                        break

Output:

uri: ['http://abcddsc779072.html']
i value: http://abcddsc779072.html
not matched
uri: ['http://abcddsc932618.html']
i value: http://abcddsc932618.html
yes matched

-- it should stop here and get next response object from DF. -- but it is again continuing with next [fields] data.

  uri: ['http://abcddsc988555.html']
    i value:  http://abcddsc988555.html
    not matched
    uri: ['http://abcddsc1094909.html']
    i value: http://abcddsc1094909.html
    not matched
2

1 Answer 1

2

You are not breaking the outer loop. Consider following changes:

    if 'documents' in json_response:
        field_data=json_response['documents']

        for row_resp_list in field_data:
            print('uri:',row_resp_list['fields']['uri'])
            match_found=False
            for i in row_resp_list['fields']['uri']:
                print('i',i)

                if uri == i:
                    print('yes matched')
                    match_found=True
                    break
                else:
                    print('not matched')

            if match_found:
                break
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much. You are awesome. worked like a charm.
Ohh, never ever got such gratitude..) My pleasure!

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.