I have a json file:-
{
"group": "avengers",
"members": [
{
"name": "Dr.Strange",
"id": 456409,
"type": "magic",
"relatives": ["wang", "the ancient one"]
},
{
"name": "Iron man",
"id": 381001,
"type": "technology",
"relatives": ["pepper pots", "james rhodes"]
}
]
}
I want the output as:-
Members from avengers group:-
| Name | ID | Type | Relatives |
|---|---|---|---|
| Dr. Strange | 456409 | magic | wang |
| Dr. Strange | 456409 | magic | the ancient one |
| Iron man | 381001 | technology | pepper pots |
| Iron man | 381001 | technology | james rhodes |
I want to get all relatives seperately. I know how to get the name, id and types because they have only 1 value.
My code:-
import json
f = open('1.json', 'r')
content = json.load(f)
for i in content['members']:
print(i['name'], end=' ')
print(i['id'], end=' ')
print(i['relatives'])
print()
Output of my code:-
Dr.Strange 456409 ['wang', 'the ancient one']
Iron man 381001 ['pepper pots', 'james rhodes']
But I want seperate relatives not in a list. So for each relative retype the name like in the table I put above.
I know that I can use i['relatives'][0] but what if I don't know the number of relatives? Maybe a for loop can help?