DocumentDB: bulkImport Stored Proc - Getting 400 Error on Array/JSON issue DocumentDB: bulkImport Stored Proc - Getting 400 Error on Array/JSON issue json json

DocumentDB: bulkImport Stored Proc - Getting 400 Error on Array/JSON issue


The "docs" must be an array of array of params, otherwise, the procedure executor will treat them as multiple params of the procedure, not a single-array-param.


the following code works when call storedProcedure to pass argument with array type.

JS:

var docs = [{'id':1},{'id':2}];executeStoredProcedure(proc, [docs])

C#

var docs = new[] {new MyDoc{id=1, source="abc"}, new MyDoc{id=2, source="abc"}];dynamic[] args = new dynamic[] {docs};ExecuteStoredProcedureAsync<int>(   procLink,   new RequestOptions {PartitionKey = new PartitionKey("abc")},   args);

NOTE: you must ensure the 'docs' have the same partition key, and pass partion key in RequestionOptions


I had the same problem. I was able to get it to work by Stringify the Array and parse it in the stored procedure. I opened an issue on the github where that code originated as well. Below is what worked for me. Good luck.

---- Stringify Array

var testArr = []for (var i = 0; i < 50; i++) {    testArr.push({        "id": "test" + i    })}var testArrStr = JSON.stringify(testArr)//pass testArrStr to stored procedure and parse in stored procedure---- Slightly altered original BulkImportexports.storedProcedure = {    id: "bulkImportArray",    serverScript:function bulkImportArray(docs) {        var context = getContext();        var collection = context.getCollection();        var docsToCreate = JSON.parse(docs)        var count = 0;        var docsLength = docsToCreate.length;        if (docsLength == 0) {            getContext().getResponse().setBody(0);        }        var totals = ""        function insertDoc(){             var msg = " count=" + count+" docsLength=" +docsLength + " typeof docsToCreate[]=" + typeof docsToCreate+ " length =" + docsToCreate.length            if(typeof docsToCreate[count] != 'undefined' ) {                 collection.createDocument(collection.getSelfLink(),                    docsToCreate[count],                    function (err, documentCreated) {                        if (err){                        // throw new Error('Error' + err.message);                        getContext().getResponse().setBody(count + " : " + err);                        }else{                           if (count < docsLength -1) {                                 count++;                                    insertDoc();                                getContext().getResponse().setBody(msg);                            } else {                                 getContext().getResponse().setBody(msg);                            }                        }                    });                 }else{                      getContext().getResponse().setBody(msg);                 }            }        insertDoc()    }}

If you want to test it in the portal Script Explorer I had to create an escaped string i.e.

var testArr = []  for(var i=200; i<250; i++){    testArr.push({"id":"test"+i})  }  var testArrStr = JSON.stringify(testArr)   console.log('"'+testArrStr.replace(/\"/g,'\\"') + '"')