0
I’m using Pymongo to make a search:
resultado = db.find_one({'nome':'xpto'})
The result of this search is a Python dictionary, but I need to convert it to a JSON. What’s the best way to do it?
0
I’m using Pymongo to make a search:
resultado = db.find_one({'nome':'xpto'})
The result of this search is a Python dictionary, but I need to convert it to a JSON. What’s the best way to do it?
3
The python Standard Library provides the module json, responsible for performing the conversion of 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
2
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)
Browser other questions tagged json python python-3.x mongodb
You are not signed in. Login or sign up in order to post.