I'm using PyMongo to do a search:
resultado = db.find_one({'nome':'xpto'})
The result of this search is a dictionary in Python, but I need to convert it to a JSON. What is the best way to do it?
I'm using PyMongo to do a search:
resultado = db.find_one({'nome':'xpto'})
The result of this search is a dictionary in Python, but I need to convert it to a JSON. What is the best way to do it?
The python Standard Library provides the json module, responsible for converting from dict to json and vice versa.
The code would look like this:
import json
resultado = db.find_one({'nome':'xpto'})
resultadoJson = json.dumps(resultado) #Convertendo dict para json
PyMongo itself provides a library to handle this situation: bson.json_util
The solution would be as follows:
from bson.json_util import dumps
resultado_json = dumps(resultado)