0

I am trying to write the following document to DocumentDb from within an Azure Function:

dynamic doc = new {
    customer = "mycust",
    version = "version2",
    document = JObject.Parse("{\"prop1\": \"Some text.\"}")
}

await 
_client.CreateDocumentAsync(UriFactory.CreateDocumentCollectionUri("db", "coll"),doc);

When saved to documentdb I get:

{
  "customer": "mycust",
  "version": "version2",
  "document": {
    "prop1": []
  },
  "id": "93ccb6d6-8829-4179-beba-606078f63dea"
}

"Some text" is missing. In the debugger it is parsed correctly.

Janusz Nowak
  • 2,595
  • 1
  • 17
  • 36
phil
  • 1,938
  • 4
  • 23
  • 33

1 Answers1

0

According to your description, I checked this issue and tested it via the following approaches, you could refer to them:

#r "Newtonsoft.Json"
using System.Net;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

public static HttpResponseMessage Run(HttpRequestMessage req,out object outputDocument,TraceWriter log)
{
    dynamic doc = new
    {
       customer = "mycust",
       version = "version2",
       document = JObject.Parse("{\"prop1\": \"Some text.\"}")
    };
    outputDocument=doc;
    return req.CreateResponse(HttpStatusCode.OK, "success");
}

Or refer to Azure WebJobs SDK Extensions for DocumentDB and configure as follows:

#r "Newtonsoft.Json"
using System.Net;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Microsoft.Azure.WebJobs.Extensions.DocumentDB;

public static HttpResponseMessage Run(HttpRequestMessage req,[DocumentDB("brucedb-2", "todoitem",ConnectionStringSetting = "brucedocumentdb-01_DOCUMENTDB")] out object outputDocument,TraceWriter log)
{
    dynamic doc = new
    {
        customer = "mycust",
        version = "version2",
        document = JObject.Parse("{\"prop1\": \"Some text.\"}")
    };
    outputDocument=doc;
    return req.CreateResponse(HttpStatusCode.OK, "success");
}

Additionally, you could also leverage Microsoft Azure DocumentDB Client Library and refer to this similar issue for adding new document to Azure DocumentDB.

Bruce Chen
  • 18,207
  • 2
  • 21
  • 35