Python Forum

Full Version: facing issue in python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i have below json file. When i amtrying to pull sid and type from applicationRoles, i am able to pull only first entry i.e.sid=123 and type=BC but i need both entries
{
"applicationgroup":[
{
"name":"test"
"applicationRoles":[
{
"sid":123
"type":'BC'
},
{
"sid":234
"type":'adBC'
}
],
"type":"tttt"
}
]
}
my code: -
import sys
import json
import csv
with open('test.json','r') as f:
json_data=json.data(f,strict=false)
with open('test.csv','w') as out:
csv_write=csv.writer(out)
csv_write.writerow(["name","sid","type"])
for json_d in json_data["applicationgroups"]:
row_array=[]
try:
row_array.append(json_d["name"])
except KeyError:
row_array.append(")
try:
row_array.append(json_d["applicationRoles"][0]["sid"])
except (KeyError,TypeError):
row_array.append(")
try:
row_array.append(json_d["applicationRoles"][0]["type"])
except (KeyError,TypeError):
row_array.append(")
except TypeError:
row_array.append(")
csv_write.writerow(row_array)
out.close()
Again, not valid json, and no indentation in the code
valid json: -
{
"applicationgroup": [{
"name": "test",
"applicationRoles": [{
"sid": 123,
"type": "BC"
},
{
"sid": 234,
"type": "adBC"
}
],
"type": "tttt"
}]
}
>>> for item in data['applicationgroup'][0]['applicationRoles']:
...     print(f"<{item['sid']}> has a type: {item['type']}")
...     
<123> has a type: BC
<234> has a type: adBC
(Nov-27-2019, 05:25 PM)snippsat Wrote: [ -> ]
>>> for item in data['applicationgroup'][0]['applicationRoles']:
...     print(f"<{item['sid']}> has a type: {item['type']}")
...     
<123> has a type: BC
<234> has a type: adBC

can you please tweak same in the code i pasted.when i am trying your solution in my code it is not working
using the code, the value am getting is something like below: -
test ,123,234,'BC','ADBC'
i want the data like below:-
test,123,BC
test,234,ADBC
i tried split function but that is also not working.
(Nov-27-2019, 05:44 PM)rajrishi990 Wrote: [ -> ]can you please tweak same in the code i pasted.when i am trying your solution in my code it is not working
You've already been asked to fix the indentation. As you can see from the lack of replies, no one wants to deal with Python code where the indentation is missing. Also, you should make an attempt to integrate the change yourself, not showing effort and asking for it to be done for you doesn't exactly encourage answerers.