This is my file1.json
:
{"count": 1,"next": null,"previous": null,"results": [{"id": 5883,"url": "https://some.api.com/api/ipam/ip-addresses/5883/","display": "1.1.1.130/24","family": {"value": 4,"label": "IPv4"},"address": "1.1.1.130/24","vrf": null,"tenant": null,"status": {"value": "active","label": "Active"},"role": null,"assigned_object_type": "dcim.interface","assigned_object_id": 801,"assigned_object": {"id": 801,"url": "https://some.api.com/api/dcim/interfaces/801/","display": "N2","device": {"id": 123,"url": "https://some.api.com/api/dcim/devices/123/","display": "A-F3-G23-15-Saeed Unit15","name": "A-F3-G23-15-Saeed Unit15"},"name": "N2","cable": null,"_occupied": false},"nat_inside": null,"nat_outside": null,"dns_name": "","description": "","tags": [],"custom_fields": {},"created": "2023-10-01","last_updated": "2023-10-01T14:05:32.001606+03:30"}]
}
This is my file2.json
:
{"count": 1,"next": null,"previous": null,"results": [{"id": 6883,"url": "https://some.api.com/api/ipam/ip-addresses/6883/","display": "2.2.2.130/24","family": {"value": 4,"label": "IPv4"},"address": "2.2.2.130/24","vrf": null,"tenant": null,"status": {"value": "active","label": "Active"},"role": null,"assigned_object_type": "dcim.interface","assigned_object_id": 901,"assigned_object": {"id": 901,"url": "https://some.api.com/api/dcim/interfaces/901/","display": "N2","device": {"id": 123,"url": "https://some.api.com/api/dcim/devices/223/","display": "A-F3-G23-16-Saeed Unit16","name": "A-F3-G23-16-Saeed Unit16"},"name": "N2","cable": null,"_occupied": false},"nat_inside": null,"nat_outside": null,"dns_name": "","description": "","tags": [],"custom_fields": {},"created": "2023-10-01","last_updated": "2023-10-01T14:05:32.001606+03:30"}]
}
I want to use a for
loop to iterate over both files (in real, there are more files with different names) and access assigned_object['name']
.
Expected output is either of these (not sure which one is applicable):
- "name": "A-F3-G23-15-Saeed Unit15"
- "name": "A-F3-G23-16-Saeed Unit16"
OR
- A-F3-G23-15-Saeed Unit15
- A-F3-G23-16-Saeed Unit16
This is my attempt (not really sure the way to properly use for
loop for files of the current path):
import jsonfile_name = 'file1.json'with open(file_name) as file:data = json.load(file)print(data['assigned_object["name"]'])
I get this error:
KeyError: 'assigned_object["name"]'