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

0
down vote
favorite
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"))

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

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

print(cb.get('healthRec').value)

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?

Thanks!

Figured it out:

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

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