i have a json data like
"preference":[
{
"access":"true1",
"visibility":"false1"
},
{
"access":"true2",
"visibility":"false2"
},
{
"access":"true3",
"visibility":"false3"
},
]
i want to access the list objects and display every list element in table format
for item in data['preference']
print(item)
TableData = [
[item.get('access'),item.get('visibility')]
]
this prints only the last item
{
"access":"true3",
"visibility":"false3"
},
in table format please suggest me some code to access every element in table format asap Thank you in Advance..
The reason for this behavior is you are not appending to the TableData
list
, you are just adding it, so as you go on adding the element to the list it overwrites the last element.
Here is the code where you will get the expected output:
data = {
"preference":[
{
"access":"true1",
"visibility":"false1"
},
{
"access":"true2",
"visibility":"false2"
},
{
"access":"true3",
"visibility":"false3"
},
]
}
TableData = []
for item in data['preference']:
print(item)
TableData.append([item.get('access'),item.get('visibility')])
print (TableData)
Output:
[['true1', 'false1'], ['true2', 'false2'], ['true3', 'false3']]