1

Im using Python, and there is no documentation on doing this in Python. I have blob storage working with python. Now I am trying to save data to the cosmos db. I have no idea what i am supposed to do in azure function?

cosmosdb_data = open(os.environ['outputDocument'], 'wb')

Would really appreciate any help on this!

EDIT:

I got it storing, but it complains that the document is corrupt anmd the _id field is missing. Does this mean you have to set your own id??

data = {
    "timestamp": datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
    "image":"path/image.jpg",
    "device":subject.split(",")[1],
    "detected":"false",
    "detection_type":"null"
}

document = open(os.environ['outputCosmosDB'], 'w')
document.write('%s' % data)
document.close()

1 Answer 1

1

document.write doesn't output valid JSON, does it? Doesn't it output single quotes, not double quotes? You need to make sure it outputs valid JSON.

_id is not necessary.

Also, Python on Azure Functions v1 is not very good and I'd recommend not using it. We're actively working on a new version of Python for v2 which will work properly for this kind of thing.

Sign up to request clarification or add additional context in comments.

4 Comments

I hope it comes soon! Let memake sure about the quotes, your probably right. I just dont know what it expects from me
It wants double quotes. When I tried your sample, the output file was single quotes, so I think that's likely the problem.
Even when its output with double quotes, as valid JSON, it gives the same error. so i make data a python dict. then document.write(json.dumps(data))
when you do output the data as a json string, it doesn't work? Could you update your question with the current code and output?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.