1

I have a list of lists (e.g. [[1,2],[3,4]]) passed from a Django view to a javascript variable and submitted with jQuery. I need to parse that variable to pull indices. The basic process is:

Add as context variable (python):

resultMsgList.append(msg)
resultMsgListJson=json.dumps(resultMsgList)
resultDict['resultMsgListJson']= resultMsgListJson

Javascript:

var resultMsgList = {{resultMsgListJson}};

    var data = {'resultMsgList':resultMsgList};
    $.post(URL, data, function(result){
    });

Google Console gives me:

Javascript:

        var resultMsgList = [["View \"S03_2005_LUZ_140814_105049_with_geom\" was successfully created!", "luz_mapfile_scen_tdm_140814_105049", "S03_2005_LUZ_140814_105049_with_geom", "C:/djangoProjects/web_output/mapfiles/ATLANTA/luz_mapfile_scen_tdm_140814_105049.map", [25, 50, 498.26708421479, 131137.057816715]]];

I copied this result to a validator, which states it is correct JSON.

The post gives me:

resultMsgList[0][]:View "S03_2005_LUZ_140814_105049_with_geom" was successfully created!
resultMsgList[0][]:luz_mapfile_scen_tdm_140814_105049
resultMsgList[0][]:S03_2005_LUZ_140814_105049_with_geom
resultMsgList[0][]:C:/djangoProjects/web_output/mapfiles/ATLANTA/luz_mapfile_scen_tdm_140814_105049.map
resultMsgList[0][4][]:25
resultMsgList[0][4][]:50
resultMsgList[0][4][]:498.26708421479
resultMsgList[0][4][]:131137.057816715

I need to get elements from this list. I currently have (python):

resultMsgListContext = request.POST.get('resultMsgListJson','')
resultMsgListContext = json.loads(resultMsgListContext)
oldMapfileName=resultMsgListContext[0][2] (+ a number of similar statements)

According to this post I then need to decode the variable in python with json.loads(), but it says there is no JSON object to be decoded. Based on the examples in the Python docs, I'm not sure why this doesn't work.

Community
  • 1
  • 1
Jason Hawkins
  • 625
  • 1
  • 9
  • 24

2 Answers2

0

I believe the problem is that it is viewing the entire resultMsgList as a string, substantiated by the fact that there is a u' xxxxx ' in the result. That's why it is saying index out of range because you're trying to access a 2D array when it is still a string. You have to convert it to an array of strings by using json.loads.

Danny Brown
  • 304
  • 1
  • 3
  • 15
  • That seems to be the problem. I used json.dumps (I believe json.loads is for decoding from JSON in python). This gives me a variable value in javascript of: '[["View \"S03_2005_LUZ_140814_092308_with_geom\" was successfully created!", "luz_mapfile_scen_tdm_140814_092308", "S03_2005_LUZ_140814_092308_with_geom", "C:/djangoProjects/web_output/mapfiles/ATLANTA/luz_mapfile_scen_tdm_140814_092308.map", [25, 50, 498.26708421479, 131137.057816715]]]';. My understanding is that this should work because it properly escapes double quotes within the list. However, it still says it's a string. – Jason Hawkins Aug 14 '14 at 15:32
  • I also tried JSON.parse on this variable, but it doesn't seem to like the quoting. – Jason Hawkins Aug 14 '14 at 15:34
  • I tried using JSON.loads on the returned value in python, but it says there isn't a JSON object to decode. – Jason Hawkins Aug 14 '14 at 15:47
0

In javascript, try passing

var data = {'resultMsgListJson':resultMsgList};

instead of

var data = {'resultMsgListJson': resultMsgListJson};

resultMsgListJson isn't a javascript variable that's defined at that point, it might be getting evaluated to undefined.

In general, in python, print the contents of resultMsgListContext before trying to do json.loads on it so you can see exactly what you're trying to parse.

ValAyal
  • 1,099
  • 2
  • 9
  • 20
  • Sorry. I corrected my question. I had var data = {'resultMsgList': resultMsgListJson}. Too many related variables with similar names. – Jason Hawkins Aug 14 '14 at 18:51
  • Did you try putting a print statement right after you retrieve request.POST.get('resultMsgListJson','') ? This will tell you exactly what you're retrieving form the POST dict (if anything). If it says there's no JSON object to decode that likely means you're not retrieving what you think you're retrieving. (Also check the variable name you're retrieving. As is, in the question, it no longer matches what you say you're sending through in the template.) – ValAyal Aug 14 '14 at 18:55