Melvin
Melvin

Reputation: 897

JObject instance expected documentdb bulk insert

I have a list which have calendar data for 3 years,i have them in excel so i used oledb to read data and store them in a list.You can find the list below

IList<BulkDateInsert> objExcelCon = ReadExcel(Filepath);

What i get in the above list

{
 "CalendarDate": "\/Date(1490985000000)\/",
 "Calendarday": "Sat",
  "isweekday": false,
  "isweekend": true
},
{
  "CalendarDate": "\/Date(1491071400000)\/",
  "Calendarday": "Sun",
  "isweekday": false,
  "isweekend": true
},
{
  "CalendarDate": "\/Date(1491157800000)\/",
  "Calendarday": "Mon",
  "isweekday": true,
  "isweekend": false
},
{
  "CalendarDate": "\/Date(1491244200000)\/",
  "Calendarday": "Tue",
  "isweekday": true,
  "isweekend": false
},
{
  "CalendarDate": "\/Date(1491330600000)\/",
  "Calendarday": "Wed",
  "isweekday": true,
  "isweekend": false
}, ..... N (upto year 2021)

I am using documentdb so i want the above list as a single document instead separate document for each day.

I get this error when i try to use createdocumentasync()

Object serialized to String. JObject instance expected.

 IList<BulkDateInsert> objExcelCon = ReadExcel(Filepath);
    JavaScriptSerializer js = new JavaScriptSerializer();
                        var json = js.Serialize(objExcelCon);
                        var ParsedJson = json.Replace("[", string.Empty).Replace("]", string.Empty);

                        await client.CreateDocumentAsync(collection.SelfLink, ParsedJson);

Need them to be inserted as a single document so that it will be easier for me to query later.

Upvotes: 0

Views: 971

Answers (1)

Fei Han
Fei Han

Reputation: 27825

I get this error when i try to use createdocumentasync() Object serialized to String. JObject instance expected.

Please don’t serialize a list object and pass it as document to CreateDocumentAsync method to create a Document for storing 3 years calendar data. If possible, as David Makogon said, please create document for each calendar entry. Besides, you could also group calendar entries into batches and create separate document for grouped calendar entries. The following simple sample is for your reference.

class Program
{
    string EndpointUri = "{endpoint URL}";
    string PrimaryKey = "{primary key}";
    DocumentClient client;

    static void Main(string[] args)
    {
        BulkDateInsert item = new BulkDateInsert() { CalendarDate = @"\/Date(1490985000000)\/", Calendarday = "Sat", isweekday = false, isweekend = true };
        BulkDateInsert item1 = new BulkDateInsert() { CalendarDate = @"\/Date(1491071400000)\/", Calendarday = "Sun", isweekday = false, isweekend = true };
        List<BulkDateInsert> list = new List<BulkDateInsert>();
        list.Add(item);
        list.Add(item1);

        IList<BulkDateInsert> objExcelCon= list;

        GroupedData gdata = new GroupedData() { Calendardata = objExcelCon };

       Program p = new Program();

        p.CreateDocumentForCalendarData(gdata).Wait();
    }

    private async Task CreateDocumentForCalendarData(GroupedData ParsedJson)
    {
        this.client = new DocumentClient(new Uri(EndpointUri), PrimaryKey);

        await client.CreateDocumentAsync("dbs/{dbname}/colls/{collectionname}", ParsedJson);
    }

public class GroupedData {

    //could be weekly, monthly data
        public IList<BulkDateInsert> Calendardata { get; set; }
    }

    public class BulkDateInsert
    {
        public string CalendarDate { get; set; }
        public string Calendarday { get; set; }
        public bool isweekday { get; set; }
        public bool isweekend { get; set; }
    }
}

Upvotes: 0

Related Questions