How do I load JSON into Couchbase Headless Server in Python?

down vote
I am trying to create a Python script that can take a JSON object and insert it into a headless Couchbase server. I have been able to successfully connect to the server and insert some data. I’d like to be able to specify the path of a JSON object and upsert that.

So far I have this:

from couchbase.bucket import Bucket
from couchbase.exceptions import CouchbaseError
import json

cb = Bucket('couchbase://XXX.XXX.XXX?password=XXXX')
print cb.server_nodes

#tempJson = json.loads(open("myData.json","r"))

       result = cb.upsert('healthRec', {'record': 'bob'})
#      result = cb.upsert('healthRec', {'record': tempJson})

except CouchbaseError as e:
        print "Couldn't upsert", e


I know that the first commented out line that loads the json is incorrect because it is expecting a string not an actual json… Can anyone help?


Figured it out:

with open('myData.json', 'r') as f:
        data = json.load(f)

        result = cb.upsert('healthRec', {'record': data})
1 Like