We are playing with JSON generation from zSecure to feed into Python and wonder about the structure of the data that's produced. This works very well and straight-foward, esp. after applying UJ98240 (thank you!).
First we wrote some newlists without a name:
fileoption dd=json fileformat=json
print dd=json
newlist type=run
sortlist datetime
newlist type=racf
s class=user cggrpnm=sys1
sortlist class profile name instdata
newlist type=racf
s class=group mask=sys1
sortlist class profile userid instdata
This produces a list of dicts, which is fair enough:
{"JSON":[
{
"DATETIME":"20Nov25 05:55:57"
}
,
{
"CLASS":"USER"
,"PROFILE":"CEA"
,"NAME":"COMMON EVENT ADAPTER"
}
,{
"CLASS":"USER"
,"PROFILE":"IBMUSER"
,"NAME":"DON'T EVER USE"
,"INSTDATA":"TOP OF GROUP TREE"
}
,
{
"CLASS":"GROUP"
,"PROFILE":"SYS1"
,"USERID":[
"IBMUSER"
,"CEA"
]
}
]}
So we could write things like
for line in data["JSON"]:
if line.get("CLASS"," ")=="USER":
print(f'{line["PROFILE"]}')
However, with more newlists added, the python code could be better structured by adding newlist names in the data:
fileoption dd=json fileformat=json
print dd=json
newlist type=run
sortlist datetime
newlist type=racf name=users
s class=user cggrpnm=sys1
sortlist class profile name instdata
newlist type=racf name=groups
s class=group mask=sys1
sortlist class profile userid instdata
but this changed the data structure in an unexpected way. Instead of creating a dict of lists, the structure has a list of dicts of dict. Note, I omitted the name of the first newlist to illustrate the asymmetry:
{"JSON":[
{
"DATETIME":"20Nov25 05:55:57"
}
,
{"USERS":
{
"CLASS":"USER"
,"PROFILE":"CEA"
,"NAME":"COMMON EVENT ADAPTER"
}
}
,{"USERS":
{
"CLASS":"USER"
,"PROFILE":"IBMUSER"
,"NAME":"DON'T EVER USE"
,"INSTDATA":"TOP OF GROUP TREE"
}
}
,
{"GROUPS":
{
"CLASS":"GROUP"
,"PROFILE":"SYS1"
,"USERID":[
"IBMUSER"
,"CEA"
]
}
}
]}
To process this you would have to concern yourself with dicts of str vs. dict of dicts, and write
for line in data["JSON"]:
if isinstance(line,dict) and "USERS" in line and isinstance(line["USERS"],dict):
print(f'{line["USERS"]["PROFILE"]}')
or, if you like to write code that you won't understand in a week from now:
for line in data["JSON"]:
if isinstance(line.get("USERS",None),dict):
print(f'{line["USERS"]["PROFILE"]}')
but it would seem to be more pythonic to produce a dict of lists, where the dict index is the newlist name:
{"JSON":{
"":[
{
"DATETIME":"20Nov25 05:55:57"
}
]
,
"USERS":[
{
"CLASS":"USER"
,"PROFILE":"CEA"
,"NAME":"COMMON EVENT ADAPTER"
}
,
{
"CLASS":"USER"
,"PROFILE":"IBMUSER"
,"NAME":"DON'T EVER USE"
,"INSTDATA":"TOP OF GROUP TREE"
}
]
,
"GROUPS":[
{
"CLASS":"GROUP"
,"PROFILE":"SYS1"
,"USERID":[
"IBMUSER"
,"CEA"
]
},
]
}}
so you could address the structure as a simple dict:
users = data["JSON"].get("USERS",[])
for u in users:
print(f'{u["PROFILE"]}')
You can, of course, create the users list from the existing list of dicts of dict structure using comprehension
users = [l["USERS"] for l in data["JSON"] if "USERS" in l]
Can anyone shed light on the reason why the dict of dicts structure was chosen?
------------------------------
Rob van Hoboken
------------------------------