Reputation: 2398
Hi I have just started experimenting with python and tornado along with mongodb(I am a newbie). I have written a simple get function to get all the values from my mongodb and return it in JSON format. The problem is when I try to write the output as a JSON string I get a trailing comma(,) after the last record from the collection.
class TypeList(APIHandler):
@gen.coroutine
def get(self):
cursor = db.vtype.find()
self.write("{"'"success"'": 1, "'"data"'":[")
while (yield cursor.fetch_next):
document = cursor.next_object()
self.write(format(JSONEncoder().encode(document)))
self.write(",")
self.write("]}")
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o,ObjectId):
return str(o)
return json.JSONEncoder.default(self, o)
And my output is like
{"success": 1, "data":[{"_id": "55a5e988545779f35d3ecdf4", "name": "fgkd", "city": "fghj"},{"_id": 12345.0, "name": "adfs", "city": "asd"},]}
Can anyone tell me how can I get rid of that trailing comma(,) after my last record, because of that comma I am getting an error malformed JSON string
I have tried using json dumps
@gen.coroutine
def get(self):
cursor = db.vtype.find({"brand": "Tata"})
while (yield cursor.fetch_next):
document = cursor.next_object()
self.write(json.dumps(document,default=json_util.default))
got the output as
{"Reg": "11ts", "_id": {"$oid": "55a5e988545779f35d3ecdf4"}, "Name": "Alex"}{"Reg": "12ts", "_id": {"$oid": "55a5eac6545779f35d3ecdf5"}, "Name": "asdf"}
When using dumps[{ "data": document }]
I am getting the output as
[{"data": {"Name": "asdf", "Reg": "asdfs", "_id": {"$oid": "55a5e988545779f35d3ecdf4"}}}]
[{"data": {"Name": "qwer", "Reg": "asdff", "_id": {"$oid": "55a5eac6545779f35d3ecdf5"}}}]
but I want the output like this
{"data": [{"Name": "asdf", "Reg": "asdfs", "_id": {"$oid": "55a5e988545779f35d3ecdf4"}},{"Name": "qwer", "Reg": "asdff", "_id": {"$oid": "55a5eac6545779f35d3ecdf5"}}]}
If I am doing something wrong please tell me I dont know how to do it.
Upvotes: 5
Views: 3982
Reputation: 50406
So your problem is with MongoDB ObjectId
? Then maybe you should have been using bson.json_util
. It's probably already installed as part of your MongoDB driver dependecies ( which all use pymongo ), but if not then install it.
import bson
import bson.json_util
from bson.json_util import dumps
from bson import ObjectId
dumps({ "a": ObjectId() })
'{"a": {"$oid": "55a782261d41c80b0432b811"}}'
Or:
dumps([{ "a": ObjectId(), "b": 1 },{ "a": ObjectId(), "b": 2 }])
'[{"a": {"$oid": "55a79543268e150463d51799"}, "b": 1}, {"a": {"$oid": "55a79543268e150463d5179a"}, "b": 2}]'
And it works just like "dumps" except all the BSON type handling is built it.
Again, no need to re-invent the wheel here and "roll your own", because people already use this.
Upvotes: 2
Reputation: 15537
Your implementation of the JSONEncoder works well. Just use it the way it was intended to be used:
>>> JSONEncoder().encode({'data': [ObjectId(), ObjectId()]})
'{"data": ["<objId>", "<objId>"]}'
The encoder will take care of serializing dicts, objects, lists, tuples, strings (including unicode), ints, longs, floats, booleans and None. Your implementation makes it aware of ObjectId
s as well. Perfect!
Just lose the string concatenation and use encode
.
Upvotes: 0
Reputation: 599630
There is no reason you should be building up JSON documents via text concatenation.
Python has a perfectly good json
module in the standard library which you should be using. Build up your document as a Python list of dicts, then use json.dumps()
to convert the whole thing to valid JSON.
Upvotes: 7