Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
facing issue in python
#1
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()
Reply
#2
Again, not valid json, and no indentation in the code
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
valid json: -
{
"applicationgroup": [{
"name": "test",
"applicationRoles": [{
"sid": 123,
"type": "BC"
},
{
"sid": 234,
"type": "adBC"
}
],
"type": "tttt"
}]
}
Reply
#4
>>> 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
Reply
#5
(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
Reply
#6
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.
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Facing issue in python regex newline match Shr 6 1,148 Oct-25-2023, 09:42 AM
Last Post: Shr
  Facing problem with Pycharm - Not getting the expected output amortal03 1 820 Sep-09-2022, 05:44 PM
Last Post: Yoriz
  Facing Problem while opening a file through command prompt vlearner 4 1,857 Jan-30-2022, 08:10 AM
Last Post: snippsat
  Facing error while executing below Python code ramu4651 1 5,633 Jan-26-2021, 06:40 PM
Last Post: ibreeden
  I want to create small multiples grouped bar plot, and facing some aesthetics issuess dev_kaur 0 1,588 Dec-05-2020, 08:49 AM
Last Post: dev_kaur
  Facing issue while saving workbook Abhisht 3 5,507 Aug-19-2020, 11:44 AM
Last Post: Larz60+
  Facing issue with pyautogui program after PC upgrade Utkarsh29 2 2,911 Jul-15-2019, 05:20 PM
Last Post: Utkarsh29
  Facing an issue in doing 'unittest.installHandler()' which will send 'stop' signal to hmarne 3 4,121 Jan-24-2017, 05:03 PM
Last Post: buran

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020