I'm fairly new to Python and have managed to convert an SQL query output into JSON. My issue is that whilst I can copy and paste the output from my code (without the strip statements) into MongoDB it works brilliantly. But for whatever reason the only way I can find to add it through the script is by injecting data between {} symbols.
The problem with this is it breaks the formatting and MongoDB rejects it. How do I get rid of the double quotes in this syntax? {"'name': 'Michael', 'address': 'Holo Ln'"}
The JSON I have outputting works perfectly as I can add it to Mongodb by hand. What I really want to be able to do is send that output directly to MongoDB without the strip statements. Any help would be appreciated.
def db():
var1 = a.recordshow()
stripped = str(var1).strip("[{").strip("}]")
collection = mydb["customers"]
mydict = { stripped }
print(mydict)
#x = collection.insert_one(mydict)
#return x
db()
def mongoinserttest():
collection = mydb["customers"]
mydict = { "name": "John", "address": "Highwatty 37" }
print(mydb.list_collection_names())
print(mydict)
mongoinserttest()
RESULT
{"'name': 'Michael', 'address': 'Holo Ln'"} # From my SQL Database.
['customers']
{'name': 'John', 'address': 'Highwatty 37'} # The correct formatting.
Error after uncommenting.
TypeError: document must be an instance of dict, bson.son.SON, bson.raw_bson.RawBSONDocument, or a type that inherits from collections.MutableMapping