From JSON file to Numpy Array
<p>I have a MongoDB collection, which, when imported to Python via PyMongo, is a dictionnary in Python.
I am looking to transform it into a Numpy Array.</p>

<p>For instance, if the JSON file looks like this :</p>

<pre><code>{
    "_id" : ObjectId("57065024c3d1132426c4dd53"),

    "B" : {
        "BA" : 14,
        "BB" : 23,
        "BC" : 32,
        "BD" : 41
    "A" : 50,
}
{
    "_id" : ObjectId("57065024c3d1132426c4dd53"),
    "A" : 1
    "B" : {
        "BA" : 1,
        "BB" : 2,
        "BC" : 3,
        "BD" : 4
</code></pre>

<p>}</p>

<p>I'd like to get in return this 5*2 Numpy Array :
    np.array([[50,14,23,32,41],
    [1,1,2,3,4]])
In that case, the first column corresponds to "A", the second one to "BA", the third one to "BB", etc.
Notice that keys are not always sorted in the same order.</p>

<p>My code, which does not work at all (and does not do what I want yet) looks like this :</p>

<pre><code>from pymongo import MongoClient
uri = "mongodb://localhost/test"
client = MongoClient(uri)
db=client.recodb
collection=db.recos

list1=list(collection.find())
array2=np.vstack([[product[key] for key in product.keys()] for product in list1])
</code></pre>

<p>Assuming you've successfully loaded that JSON into Python, here's one way to create the Numpy array you want. My code has a minimal definition of <code>ObjectId</code> so that it won't raise a NameError on <code>ObjectId</code> entries. </p>

<pre><code>sorted(d["B"].items())]
</code></pre>

<p>produces a list of (key, value) tuples from the contents of a "B" dictionary, sorted by key. We then extract just the values from those tuples into a list, and append that list to a list containing the value from the "A" item.</p>

<pre><code>import numpy as np

class ObjectId(object):
    def __init__(self, objectid):
        self.objectid = objectid

    def __repr__(self):
        return 'ObjectId("{}")'.format(self.objectid)

data = [
    {
        "_id" : ObjectId("57065024c3d1132426c4dd53"),
        "B" : {
            "BA" : 14,
            "BB" : 23,
            "BC" : 32,
            "BD" : 41
        },
        "A" : 50
    },
    {
        "_id" : ObjectId("57065024c3d1132426c4dd53"),
        "A" : 1,
        "B" : {
            "BA" : 1,
            "BB" : 2,
            "BC" : 3,
            "BD" : 4
        }
    }
]

array2 = np.array([[d["A"]] + [v for _, v in sorted(d["B"].items())] for d in data])
print(array2)
</code></pre>

<p><strong>output</strong></p>

<pre><code>[[50 14 23 32 41]
 [ 1  1  2  3  4]]
</code></pre>

python arrays json mongodb numpy