Search code examples
pythonjsonapachecherrypy

How to handle multiple line JSON from Python under Apache


We are having trouble handling multiple line JSON under Apache (Specifically, we are using modwsgi). When showing a table on a web page, a Python CherryPy server in the backend provides a string formed by JSON objects separated by the \n character. The string is further processed on the Apache side to create the table. The problem is that Apache only considers the first JSON object and does not show the rest of the lines in the string.

Oddly enough, the response is correct when we send the request directly to the CherryPy server.

The Python code on the CherryPy application returns as response:

    resquery.insert(0,json.dumps(orderedhead))
    return "\n".join( [json.dump(element) for element in list_of_elements ] )

Solution

  • The problem is that you're not returning a well formed JSON object, you're sending a lot of different JSON objects with some text in between (the newline).

    You can only send a single JSON object in a response - if you need to send a lot of data, you can wrap it in a larger structure:

    resquery.insert(0,json.dumps(orderedhead))
    return json.dump(list_of_elements)
    

    Now each of your elements will be a member of a larger JSON list, thus making it only one object.