Python Forum
facing issue in python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: facing issue in python (/thread-22798.html)



facing issue in python - rajrishi990 - Nov-27-2019

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



RE: facing issue in python - buran - Nov-27-2019

Again, not valid json, and no indentation in the code


RE: facing issue in python - rajrishi990 - Nov-27-2019

valid json: -
{
"applicationgroup": [{
"name": "test",
"applicationRoles": [{
"sid": 123,
"type": "BC"
},
{
"sid": 234,
"type": "adBC"
}
],
"type": "tttt"
}]
}


RE: facing issue in python - snippsat - Nov-27-2019

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



RE: facing issue in python - rajrishi990 - Nov-27-2019

(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


RE: facing issue in python - rajrishi990 - Nov-28-2019

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.


RE: facing issue in python - micseydel - Dec-05-2019

(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.