Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON serializing Mongodb

I am using the python package pymongo to retrieve data from a mongodb database.

>>> r = collection.find()   # returns an object of class 'Cursor' 

Then I convert to a list

>>> l = list(r)             # returns a 'list' of 'dict' 

here is what print(l) returns:

>>> [{u'date': datetime.datetime(2009, 11, 10, 10, 45), u'_id': 1, u'name': u'name1', u'value': 11},{u'date': datetime.datetime(2013, 11, 10, 10, 45), u'_id': 2, u'name': u'name2', u'value': 22}] 

Now I need to convert to JSON so that I can manipulate it.

>>> json.dumps(l) Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/lib/python2.7/json/__init__.py", line 231, in dumps     return _default_encoder.encode(obj)   File "/usr/lib/python2.7/json/encoder.py", line 201, in encode     chunks = self.iterencode(o, _one_shot=True)   File "/usr/lib/python2.7/json/encoder.py", line 264, in iterencode     return _iterencode(o, 0)   File "/usr/lib/python2.7/json/encoder.py", line 178, in default     raise TypeError(repr(o) + " is not JSON serializable") TypeError: datetime.datetime(2009, 11, 12, 11, 14) is not JSON serializable 

I have also tried to follow http://api.mongodb.org/python/1.7/api/pymongo/json_util.html without success: Edit: the recent version of the link is http://api.mongodb.org/python/current/api/bson/json_util.html

>>> json.dumps(l, default=json_util.default)   Traceback (most recent call last):     File "<stdin>", line 1, in <module>   NameError: name 'json_util' is not defined   

Note: precisely I need to push this result to R using the R package rPython and its function rPython::python.get("l")

Side Question: What is the u (u'Date', u'name', etc..) before each field in the list of dict?

like image 275
RockScience Avatar asked Oct 30 '13 05:10

RockScience


People also ask

Can JSON be stored in MongoDB?

Does MongoDB use BSON, or JSON? MongoDB stores data in BSON format both internally, and over the network, but that doesn't mean you can't think of MongoDB as a JSON database. Anything you can represent in JSON can be natively stored in MongoDB, and retrieved just as easily in JSON.

Is JSON serialized data?

JSON is a format that encodes objects in a string. Serialization means to convert an object into that string, and deserialization is its inverse operation (convert string -> object).

How do I import a JSON file into MongoDB?

To import JSON file you need to follow the following steps: Step 1: Open a command prompt and give command mongod to connect with MongoDB server and don't close this cmd to stay connected to the server. Step 2: Open another command prompt and run the mongo shell. Using the mongo command.

What is JSON format in MongoDB?

JSON is a data format that represents the values of objects, arrays, numbers, strings, booleans, and nulls. The Extended JSON format defines a reserved set of keys prefixed with " $ " to represent field type information that directly corresponds to each type in BSON, the format that MongoDB uses to store data.


1 Answers

The pymongo documentation you pointed is obsolete. If you're using version 1.7 I recommend updating. With a more recent version you can do this:

from bson.json_util import dumps  dumps(l) 

https://pymongo.readthedocs.io/en/stable/api/bson/json_util.html

Side answer: u'name', u'date', u'_id' etc are the names of the fields of the document on the database.

like image 54
Rafa Avatar answered Sep 23 '22 09:09

Rafa