Search code examples
pythonstringdictionaryintegerindices

Keep getting type error python


I am doing a search query with jqGrid. My parameters are as follows:

function:GL
_search:true
nd:1446209493437
rows:50
page:1
sidx:datetime
sord:asc
filters:{"groupOp":"AND","rules":[{"field":"username","op":"eq","data":"user"},{"field":"description","op":"nc","data":"utility"}]}
searchField:
searchString:
searchOper:

When I run the script locally using these values it works fine but when I use the web page and cgi:

C:\wamp\www\ISaidItBest\assets\cgi-bin\app\Log.py in getAllLogs(self=<app.Log.Log object>)
     91                 elif filters:   # filter options
     92                     buildwhere = ""
=>   93                     rules = filters['rules']
     94                     for idx in range(len(rules)):
     95                         field = rules[idx]['field']
rules undefined, filters = '{"groupOp":"AND","rules":[{"field":"username","o...ield":"description","op":"nc","data":"utility"}]}'
TypeError: string indices must be integers 
      args = ('string indices must be integers',) 
      with_traceback = <built-in method with_traceback of TypeError object>

Solution

  • You need to convert filters from str to dict. Read this: https://docs.python.org/2/library/json.html#json.loads

    try:

    ....
    buildwhere = ""
    if isinstance(filters, str):
        filters = json.loads(filters)
    ....