how to access objects in python with for loop in different files

2024/11/8 0:35:20

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):

  1. "name": "A-F3-G23-15-Saeed Unit15"
  2. "name": "A-F3-G23-16-Saeed Unit16"

OR

  1. A-F3-G23-15-Saeed Unit15
  2. 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"]'
Answer
import jsonfile_name = 'file1.json'with open(file_name) as file:data = json.load(file)data["results"][0]["assigned_object"]["device"]["name"]A-F3-G23-15-Saeed Unit15
https://en.xdnf.cn/q/120681.html

Related Q&A

multiplicative digital root of a number using loops

I need to find the multiplicative digital root of a number in python using only loops.something that does the same as the below code but using loops:print("multiplicative digital root of a number …

How to access key values in a json files dictionaries with python

I have a script that pulls json data from an api, and I want it to then after pulling said data, decode and pick which tags to store into a db. Right now I just need to get the script to return specifi…

How to use sin(x) and cos(x) functions with eval

I need a program which can make graphs by matplotlib with functions I write in the console. But it doesnt work with trigonometric functions. The code I already wrote is:from numpy import linspace impo…

python - whats the difference between = and ==? [duplicate]

This question already has answers here:What do the symbols "=" and "==" mean in python? When is each used?(5 answers)Closed 5 years ago.I wonder know whats the difference between …

Sorting images by dates into a list from a dictionary

I know I asked this before, but Im still not sure why I just get an empty list when I test thisdef sorted_images(image_dict): (dict) -> list of strGiven an image dictionary return a list of the file…

multiple search and replace in python

I need to search in a parent folder all files that are config.xml and in those files replace one string in another. (from this-is to where-as)

how do I convert the first letter of every word in a list from upper case to lower case? [duplicate]

This question already has answers here:How to downcase the first character of a string?(9 answers)Closed 6 years ago.how do I convert the first letter of each of the below from upper case to lowere ca…

Ubuntu 11.04: Installing PIL into a virtualenv with PIP [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

What does this error mean ? Expected an indented block Python [duplicate]

This question already has answers here:Im getting an IndentationError (or a TabError). How do I fix it?(6 answers)Closed 7 months ago.My code is the following:def value(one,two): if one < two: retu…

Why is len(file.read()) giving me a value of zero?

Why are the values of print len() different for both functions? Are they not the same?The file this script is opening was a text file with three lines of text. i named it test.txt and inside it was J…