Importing formatted JSON from SQL into MongoDB

Viewed 20

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
1 Answers

It appears I have fixed my problem by converting a string dictionary into a dictionary object with ast.literal_eval().

MongoDB seems like they should just have implemented native JSON support rather than BSON/Python dictionary. This seems like an obvious short fall to me. Was about to move to CouchDB for it's native curl support.

def db():
    var1 = a.recordshow()
    stripped = str(var1).strip("[").strip("]")
    dicto = ast.literal_eval(stripped)
    collection = mydb["customers"]
    mydict = var1
    print(var1)
    x = collection.insert_one(dicto)
    return x

db()
Related