This is json file:
json
{ "results": [ { "url": "https://api.server.com/cables/100/", "termination_a": { "url": "https://api.server.com/interfaces/250/", "device": { "url": "https://api.server.com/devices/10/", "display": "device-number1-2023-08 myname (1718)", "name": "device-number1-2023-08 myname" } } }, { "url": "https://api.server.com/cables/200/", "termination_a": { "url": "https://api.server.com/interfaces/160/", "device": { "url": "https://api.server.com/devices/22/", "display": "device-number3-2023-08 myname (2245)", "name": "device-number3-2023-08 myname" } } }, { "url": "https://api.server.com/cables/300/", "termination_a": { "url": "https://api.server.com/interfaces/260/", "device": { "url": "https://api.server.com/devices/73/", "display": "device-number8-2023-08 myname (3678)", "name": "device-number8-2023-08 myname" } } } ] }
This is txt file:
txt
this is device-number1-2023-08 myname1 and it is good. this is device-number8-2023-08 myname8 and it is. this is device-number3-2023-08 myname3 and it is not good.
This is my python code:
python
import json with open('test.json', 'r') as j: data = json.load(j) with open('test.txt', 'r') as f: for ff in f.readlines(): ff = ff.split(' ') for jj in data['results']: jj_name = jj['termination_a']['device']['name'] ff_name = f'{ff[2]} {ff[3]}' if jj_name == ff_name: print(f'jj_name --> {jj_name}, ff_name --> {ff_name}')
This is what I’m going to do:
if jj_name == ff_name, then print `url` of that name from json file and print ['termination_a']['url'] of that name from json file
This is expected output:
jj_name --> device-number1-2023-08 myname1, ii_name --> device-number1-2023-08 myname1, url https://api.server.com/cables/100/, ['termination_a']['url'] --> https://api.server.com/interfaces/250/ jj_name --> device-number3-2023-08 myname3, ii_name --> device-number3-2023-08 myname3, url https://api.server.com/cables/200/, ['termination_a']['url'] --> https://api.server.com/interfaces/160/ jj_name --> device-number8-2023-08 myname8, ii_name --> device-number8-2023-08 myname8, url https://api.server.com/cables/300/, ['termination_a']['url'] --> https://api.server.com/interfaces/260/
It looks like you want to match names from the text file with names in the JSON file and then print the corresponding URLs from the JSON data. Here’s how you can modify your Python code to achieve that:
import json with open('test.json', 'r') as j: data = json.load(j) with open('test.txt', 'r') as f: for ff in f.readlines(): ff = ff.split(' ') ff_name = f'{ff[2]} {ff[3]}' for jj in data['results']: jj_name = jj['termination_a']['device']['name'] if jj_name == ff_name: url = jj['url'] term_a_url = jj['termination_a']['url'] print(f'jj_name --> {jj_name}, ii_name --> {ff_name}, url {url}, [\'termination_a\'][\'url\'] --> {term_a_url}')
This code will output the expected result:
jj_name --> device-number1-2023-08 myname1, ii_name --> device-number1-2023-08 myname1, url https://api.server.com/cables/100/, ['termination_a']['url'] --> https://api.server.com/interfaces/250/ jj_name --> device-number3-2023-08 myname3, ii_name --> device-number3-2023-08 myname3, url https://api.server.com/cables/200/, ['termination_a']['url'] --> https://api.server.com/interfaces/160/ jj_name --> device-number8-2023-08 myname8, ii_name --> device-number8-2023-0