More efficient way to create JSON from Python
<p>I'd like to write an API that reads from a CSV on disk (with x, y coordinates) and outputs them in JSON format to be rendered by a web front end. The issue is that there are lots of data points (order of 30k) and so going from numpy arrays of x and y into JSON is really slow.</p>

<p>This is my current function to get the data in JSON format. Is there any way to speed this up? It seems very redundant to have such a large data structure for each 2d point.</p>

<pre><code>def to_json(xdata, ydata):
    data = []
    for x, y in zip(xdata, ydata):
        data.append({"x": x, "y": y})
    return data
</code></pre>

<p>Your method seems reasonable enough. Here are a few changes I might make to it. The itertools module has lots of handy tools that can make your life easier.  I used izip, which you can read up on <a href="https://docs.python.org/2/library/itertools.html#itertools.izip" rel="nofollow">here</a> </p>

<pre><code>import json
from itertools import izip

def to_json(xdata, ydata):
  data = []
  for x, y in izip(xdata, ydata): # using izip is more memory efficient
    data.append({"x": x, "y": y})
  return json.dumps(data) # convert that list into json
</code></pre>

python json rest