My json file look likes this and I'm trying to access the element syslog
in a for loop.
{
"cleanup":{
"folderpath":"/home/FBML7HR/logs",
"logfilename":""
},
"preparation":{
"configuration":{
"src_configfile":"src.cfg",
"dest_configfile":"/var/home/FBML7HR/etc/vxn.cfg"
},
"executable_info1":[
{
"login_info":{
"hostname":"10.4.0.xxx",
"username":"***",
"password":"***"
}
},
{
"command":{
"folderpath":"/var/home/FBML7HR/SrcCode/vxnservers/fdchost/north/test/hostsim/",
"processname":"northhostsim",
"parameters":"-d"
}
}
],
"executable_info2":[
{
"login_info":{
"hostname":"10.4.0.xxx",
"username":"***",
"password":"***"
}
},
{
"command":{
"folderpath":"/var/home/FBML7HR/SrcCode/vxnservers/fdchost/north/build/Linux-2.6.18-194.8.1.el5/bin",
"processname":"northhost",
"parameters":"-s brazil -d"
}
}
],
"executable_info3":[
{
"login_info":{
"hostname":"10.4.0.xxx",
"username":"***",
"password":"***"
}
},
{
"command":{
"folderpath":"cd /var/home/xxx/SrcCode/vxnservers/fdchost/north/test/vxnclient_mt",
"processname":"vxnclient_north_mt",
"parameters":"0 320 205 14897 16880 60000 60000 2 2"
}
}
]
},
"execution":[
{
"test_case":{
"scriptname":"/var/home/FBML7HR/test/testcase1.sh",
"testreport":{
"syslog":"/var/log/messages",
"backupsyslog":"backuplogs1.txt",
"clientsimlog":"/var/home/FBML7HR/test/out.log",
"backupclientsimlog":"Clientlogs1.txt"
}
}
},
{
"test_case":{
"scriptname":"/var/home/FBML7HR/test/testcase2.sh",
"testreport":{
"syslog":"/var/log/messages",
"backupsyslog":"backuplogs2.txt",
"clientsimlog":"/var/home/FBML7HR/test/out.log",
"backupclientsimlog":"Clientlogs2.txt"
}
}
}
],
"verification":{
"testreport":{
"syslog":"/var/log/messages",
"backupsyslog":"backuplogs.txt",
"reportfilename":"/var/home/FBML7HR/test/out.log",
"backuplogfile":"Clientlogs.txt"
}
}
}
I do it like this:
for i in data['execution']:
cmd = data['execution'][i]['test_case']['scriptname']
But I get the error saying "TypeError: list indices must be integers, not dict". I'm new to python (and json as well). Could anybody suggest where I'm going wrong ?
The Python "TypeError: list indices must be integers or slices, not dict" occurs when we use a dictionary to access a list at a specific index. To solve the error, use an integer or a slice for list indexes, e.g. my_list[0] .
The Python "TypeError: list indices must be integers or slices, not str" occurs when we use a string instead of an integer to access a list at a specific index. To solve the error, use the int() class to convert the string to an integer, e.g. my_list[int(my_str)] .
The Python "TypeError: list indices must be integers or slices, not tuple" occurs when we pass a tuple between the square brackets when accessing a list at index. To solve the error, make sure to separate nested list elements with commas and correct the index accessor.
What is a list of dictionaries in Python? We all know in Python, a list is a linear data structure that can store a collection of values in an ordered manner. And these values can be any Python object. A dictionary is also a Python object which stores the data in the key:value format.
You are looping over the values in the list referenced by data['execution']
, not indices.
Just use those values (dictionaries) directly:
for i in data['execution']:
cmd = i['test_case']['scriptname']
You probably want to give that a more meaningful loop name:
for entry in data['execution']:
cmd = entry['test_case']['scriptname']
dataArray=data['execution']
for i in range(len(dataArray)):
cmd = dataArray[i]['test_case']['scriptname']
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With