How to modify and then recombine json response data without breaking json formatting

522 views
Skip to first unread message

Henry Versemann

unread,
Mar 9, 2015, 1:18:11 PM3/9/15
to django...@googlegroups.com
First to be clear up front let me say that I'm using Django1.7, Python 2.7.8, and the requests (Requests: HTTP for Humans) library version2.4.3 to build the application mentioned below.

I have an application which needs to be able to process a single AJAX request as a series of related sub-requests sent to a remote API. Each sub-request response comes back in JSON format. The first of these sub-requests returns a list of students in a particular course, and the returned response data is a list containing student objects all in JSON format. A Django view code then strips outs the 'name' and 'id' of each student object in the list and builds it as an entry in another python list to be used later(I'm using "json.loads()" to get to successfully the original JSON data returned in each reasponse). Then once this list is built it is passed to another view which then submits a different request, for each student "id", in the newly built list, to the same API, to return all student information (related to a particular course-id which is also passed in this second request), for the current student. Then the returned course-student information, for this second request, is also returned in JSON format.

Everything up to this point works perfectly, but it is at this point that I believe my process is going wrong, when I try to modify the JSON data returned, form the second series of requests.

My modifications seem to work, on the server side(and I'm using json.dumps() to serialize my data into JSON format), and all of my data seems to be present when I print it in my command prompt window right before sending it back to the client, as part of an HttpResponse. Then when I try to access some of the data using javascript/jQuery once its been sent back to the client then some of the  "jQuery.parseJSON()" statements fail when I try to access the data I've formatted in the view, for my response.

So my next question is how do I correctly modify the JSON response data (for each course student) returned from each request? Then once modified how do I correctly add it to a list, without breaking the JSON formatting, and causing it to not be well-formed, when I send it back to the client?

This is the first time that I've attempted to do anything like this (modify and/or recombine) with JSON data, so I'm looking for an answer that my current level of Django/Python experience can't seem to provide, though I am continuing to search for answers.

Any suggestions will be greatly appreciated.

Thanks for the help.

Henry

Bill Freeman

unread,
Mar 9, 2015, 1:42:23 PM3/9/15
to django-users
Did you remember to set the content type of your response to application/json?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/790b2c45-edc8-4528-9faa-4cecfc072626%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Henry Versemann

unread,
Mar 9, 2015, 1:50:03 PM3/9/15
to django...@googlegroups.com
Yes Id did do that. The problems I seem to be having only seem to happen whenever I modify the data before sending it. For many other requests that I'm sending and not modifying the data, before I send them back to the client I have no problems and everything is apparently parsed out ok by the javascript on the client side.
Thanks.

Henry  

Bill Freeman

unread,
Mar 9, 2015, 2:04:12 PM3/9/15
to django-users
If it's not the basics, then you haven't provided enough information to allow someone to spot the problem.  If you post the code that is performing the modification, someone may be able to spot the issue.

Message has been deleted

Henry Versemann

unread,
Mar 9, 2015, 3:35:31 PM3/9/15
to django...@googlegroups.com

OK here goes. The logic in the routine which I actually doing the modification to each individual JSON response looks like this:

        if str(objctTyp) == 'user':
            rspnsdata['name'] = lstobjct['name']
            rspnsdata['id'] = lstobjct['id']


The routine which is doing this is receiving parameter data that looks like this when printed in string format in the windows command prompt window:

The following piece of parameter data is actually what came back as my JSON response from the current secondary request that the application sent.
rspnsdata=

{
    u'participations': [],
    u'page_views':
    {
        u'2015-02-05T00:00:00-06:00': 1,
        u'2015-03-02T23:00:00-06:00': 1,
        u'2015-01-24T19:00:00-06:00': 1,
        u'2015-02-08T13:00:00-06:00': 1,
        u'2015-01-28T19:00:00-06:00': 1,
        u'2015-01-20T19:00:00-06:00': 1,
        u'2015-02-19T10:00:00-06:00': 2,
        u'2015-02-21T17:00:00-06:00': 1,
        u'2015-02-02T13:00:00-06:00': 1,
        u'2015-02-16T13:00:00-06:00': 1,
        u'2015-02-01T23:00:00-06:00': 2,
        u'2015-02-03T12:00:00-06:00': 2
    }

}

The following bit of parameter data was actually passed in as the data for the current student which the latest secondary request was sent for and whose associated data JSON data is shown above.

lstobjct=

{
    'name': 'Brennan Kennedy',
    'id':'8202'
}

The following parameter data type "user" indicates that 'name' and 'id' key data is passed in in the 'lstobjct' parameter and those key value pairs will be added to the received JSON data.

objctTyp=

user

Hope this helps.
Thanks.

Henry

Bill Freeman

unread,
Mar 10, 2015, 11:12:24 AM3/10/15
to django-users
OK.  but I need more code context.  The if statement that does the modification is clearly python, so at that point rspnsdata must be a python dictionary, not a JSON string.  Yet, if I understand you correctly, it is JSON to begin with (and you are using json.loads() to turn it into python data so that you can manipulate it, but then you must later turn it back into a JSON string (json.dumps()) to send as a response.  So there's lots of code you haven't shown.  If the problem were where you're concentrating, you would have found it.  Please expand the scope of what you're showing.

Henry Versemann

unread,
Mar 11, 2015, 10:32:10 AM3/11/15
to django...@googlegroups.com
I have attached files (please pardon the code file; they may have a lot of comments or commented out code in them) to this response so you can look at the data that I'm seeing as well as the actual code which is using some of the data shown (the file json_data_list_031115.txt is gotten in a previously executed view and then sent as a parameter to the SetupComplexResponseData view included in file SetupComplexResponseData_031115.txt ). The json_data_list_031115.txt is a combination of name and id data scraped from a previous request and shown in the object_parm_list of objects in the response_data_031115.txt file. Note the "name" and "id" fields which some of my code previously added to the original response data returned in each of the object in the json_data_list in the json_data_list_031115.txt file, and note the difference in the way the keys "participations" and "page_views" of each object are formatted as opposed to the two new keys "name" and "id" that my code added to the individual json response data now in python object form. The final file  Send_Request_View_Code_Block_031115.txt show the higher level code which calls the SetupComplexresponseData view and also then returns the response data back to the client (return HttpResponse()). If you have any questions or need more stuff to look at let me know and I'll post it. I'm heading out to a meeting now which I may be at for several hours.
response_data_031115.txt
json_data_list_031115.txt
SetupComplexResponseData_031115.txt
Send_Request_View_Code_Block_031115.txt
Reply all
Reply to author
Forward
0 new messages