How do I convert a string that is valid json returned from a psycopg2 query into json that can be inserted to a mongo instance via pymongo?

StackOverflow https://stackoverflow.com/questions/19554261

Question

How do I convert a string that is valid json returned from a psycopg2 query into json that can be inserted to a mongo instance via pymongo? I've tried a number of things, all of them unsuccessful. I keep getting a TypeError on the insert to mongo TypeError: 'str' object does not support item assignment.

I'm using Python 2.7 and psycopg2 2.4.5, and pymongo 2.1 by the way. I can upgrade if necessary, I just went with what installed by default using Ubuntu and apt-get.

import psycopg2
import pymongo
from pymongo import Connection
import ast
import json

connection = Connection()
db = connection['af_reports_test']
psa = db['psa']


cur = con.cursor()
cur.execute("SELECT activity_json FROM \"edus\".\"ACTIVITIES\" WHERE         status='PUBLISHED'")
results = cur.fetchall()


for rec in results:
    for value in rec:   
        psa.insert(value)

Here is a sample of what I'm getting back from the database:

{"activity":{"to":{"users":["someone","someoneElse"]}}}

Also, here is the traceback:

Traceback (most recent call last):
  File "getPSA.py", line 33, in <module>
    psa.insert(activity)
  File "/usr/lib/python2.7/dist-packages/pymongo/collection.py", line 300, in insert
    docs = [self.__database._fix_incoming(doc, self) for doc in docs]
  File "/usr/lib/python2.7/dist-packages/pymongo/database.py", line 252, in _fix_incoming
    son = manipulator.transform_incoming(son, collection)
  File "/usr/lib/python2.7/dist-packages/pymongo/son_manipulator.py", line 73, in transform_incoming
    son["_id"] = ObjectId()
TypeError: 'str' object does not support item assignment
Was it helpful?

Solution

I'm just guessing here, since you didn't give us the traceback or enough information to reproduce your problem, but I suspect it's this:

activity = ast.literal_eval(json.dumps(value))  

You're taking value, which is presumably some kind of Python object, calling json.dumps to turn it into a JSON string representing that object, then calling ast.literal_eval to turn it back into the original object. That can't possibly be useful.

Meanwhile, the error seems to be saying that you have a string when it's expecting some other kind of object (most likely a list or other mutable sequence). If value is already a JSON string, you want to use json.loads to get a Python object out of it. If it's a Python literal string itself, you want to use ast.literal_eval on the string, not on a JSON string representing the original string. If it's some other thing… well, we'd need to know what other thing to know how to decode it. The only thing I can be sure of is that there's no conceivable thing it could be for which your code would be useful.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top