I have the following Json file
{
"AFB": [
{
"policy": [
{
"Systems": [
{
"icon": "icon_sys"
},
{
"scripts": [
"os_preg4",
"os_preg14"
]
}
]
},
{
"Sudo": [
{
"icon": "icon_sys"
},
{
"scripts": [
"sudo_preg4",
"sudo_preg14"
]
}
]
}
]
}
],
"INFRA": [
{
"policy": [
{
"Systems": [
{
"icon": "icon_sys"
},
{
"scripts": [
"os_preg4",
"os_preg14"
]
}
]
}
]
}
]
}
And I try to read and go through a dictionary list, inside the other dictionary list, but I don't get what I want, what I want is to save the value of the icon in a variable, and in another variable the value of the scripts. for now I have this that if it works for me, it prints the client that I passed as a key and the policies, with their respective dictionaries, and I can no longer
import json
with open('/home/lopuma/Compliance/.conf/clientes.json') as op:
data = json.load(op)
for clt in data['AFB']:
print("customer {}, policy {}".format('AFB',clt['policy']))
for pol in clt['policy']:
print("policy, en linea : ", pol)
for sis_pol in pol:
print("Sis : ", sis_pol)
for policy in pol[sis_pol]:
lista = list(policy.keys())
print("list : ", lista)
I add the output of the command, but I don't know how to get only the value of the icon in one variable and only the value of each item in the list of scripts in another.
what I have tried is to go through the dictionary list to, show the result of a client and get its policies (key), within its policies, either sudo or systems, I want to get the value of icon in a variable and the in another variable the script value
I NEED TO GET SOMETHING LIKE THIS
I need to save to a variable, the icon, and the value of the script, for example:
Customer: "AFB"
-------------------------
**policy 'Systems'**
---------------------
icon = "icon_sys"
var_script = ["os_preg4", "os_preg14"]
**Sudo Policy**
---------------------
var_icon = "icon_sudo"
var_script = ["sudo_preg4", "sudo_preg14"]
And so with each of the clients, either AFB or INFRA
CodePudding user response:
I hope this code does what you asked for.
data = json.loads(op)
icons = []
scripts = []
for customer in data:
print(f"{'_' * 50}\nCustomer: {customer}\n {'-' * 50}")
for politica in data[customer][0]["politica"]:
politica_key = list(politica.keys())[0]
print(f"**Policy {politica_key}**\n {'-' * 50}")
for item in list(politica.values())[0]:
item_key = list(item.keys())[0]
if item_key == "icon":
print(f"Icon = {item[item_key]}")
elif item_key == "scripts":
print(f"Scripts = {item[item_key]}")
print()
Output -
__________________________________________________
Customer: AFB
--------------------------------------------------
**Policy Sistemas**
--------------------------------------------------
Icon = icon_sys
Scripts = ['os_preg4', 'os_preg14']
**Policy Sudo**
--------------------------------------------------
Icon = icon_sys
Scripts = ['sudo_preg4', 'sudo_preg14']
__________________________________________________
Customer: INFRA
--------------------------------------------------
**Policy Sistemas**
--------------------------------------------------
Icon = icon_sys
Scripts = ['os_preg4', 'os_preg14']
CodePudding user response:
If the json format will always be the same, here is how I got it into a dictionary for you which should be easy to parse:
with open('/home/lopuma/Compliance/.conf/clientes.json') as file:
data = json.load(file)
newdata = {}
for item in data:
newdata[item] = {
"icon": data[item][0]["politica"][0]["Sistemas"][0]["icon"],
"scripts": data[item][0]["politica"][0]["Sistemas"][1]["scripts"]
}
Then you should be able to get them out of the dictionary like: newdata['AFB']['icon'] and newdata['afb']['scripts']