Search code examples
pythoncassandrapycassa

Python Append JSON list within for loop


I have the following Python code which takes data from cassandra database. What i want to achieve is to have json encoded variable at the end. However, not surprisingly, my code only attaches the latest data row to the variable.

How can I combine or append all of the data to a single variable?

for key, columns in crime.get_range():
    data = json.dumps(columns)

Thanks

I am using Pycassa as a library to access Cassandra


Solution

  • Collect all the data in a list first, then encode that list:

    data = json.dumps([columns for key, columns in crime.get_range()])