Mar-10-2022, 01:07 PM
(Mar-10-2022, 12:41 PM)snippsat Wrote:(Mar-10-2022, 11:09 AM)ilknurg Wrote: For parsing it, im trying to write this content in a JSON file and i tried read and parse it.It's already a data structure so making it JSON and read just a extra unnecessary step.
Just a copy of your data and make into alst
variable.
lst = [ [{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"}], [{"Index": "1", "IPAddress": ["192.168.1.240,fe80::350e:d28d:14a5:5cbb"]}], [{"Name": "DC01", "UserName": None}], ]>>> for i in lst: ... print(i[0].get('Name')) ... DC01 None DC01 >>> for i in lst: ... print(i[0].get("Manufacturer")) ... VMware, Inc. None NoneAgain just a copy of your data and the extra JSON step.
import json with open('data.json', 'r') as myfile: data = json.load(myfile)>>> for i in data: ... print(i[0].get('Name')) ... DC01 None DC01 >>> for i in data: ... print(i[0].get("Manufacturer")) ... VMware, Inc. None None
Thank you. The code will work with the json you wrote.
I want to ask one more thing.
My json is coming me like :
['[{"Manufacturer": "VMware, Inc.", "Model": "VMware7,1", "Name": "DC01"}]', '[{"Index": "1", "IPAddress": ["192.168.1.240,fe80::350e:d28d:14a5:5cbb"]}]', '[{"Name": "DC01", "UserName": null}]']How can i reorganize it for working the code you gave me?