I am trying to add an entity to Dynamics 2016 On Premise using the Web API. The Entity references three other entities. When I try to post the Entity, I receive the following error.
An unexpected 'StartArray' node was found when reading from the JSON reader. A 'StartObject' node was expected.
This error occurs on the three entities I am referencing: ccseq_clientid, ccseq_employeeid, ccseq_setid.
I have looked at this question and this question, but without success.
What does this error mean and how can I resolve it?
Variables
public contentType: string = "application/json;";
public dataType: string = "json";
public expenseTransaction: string = this.connection + "ccseq_expensetransactions";
public expenseTransactionSet: string = this.connection + "ccseq_expensetransactionsets";
public client: string = this.connection + "ccseq_clients";
public systemUser: string = this.connection + "systemusers";
Ajax Call
Create(expenses: Array<ExpenseTransactionBase>): void {
for (let i: number = 0; i < expenses.length; i++) {
$.ajax({
url: this.expenseTransaction,
type: "POST",
contentType: this.contentType,
accepts: this.contentType,
dataType: this.dataType,
data: JSON.stringify(expenses[i].toJSON()),
success: function (data: any): void {
alert("Success");
},
error: function (data: any) {
alert("error");
}
});
}
};
JSON Creation
toJSON(): any[] {
let json = [];
json[0] = {
"[email protected]": this.client + "(" + this.ccseq_clientid + ")",
"ccseq_clientnumber": this.ccseq_clientnumber,
"ccseq_employeefirstname": this.ccseq_employeefirstname,
"ccseq_employeelastname": this.ccseq_employeelastname,
"[email protected]": this.systemUser + "(" + this.ccseq_employeeid + ")",
"ccseq_expenseerrorcode": this.ccseq_expenseerrorcode,
"ccseq_generalledgername": this.ccseq_generalledgername,
"ccseq_generalledgernumber": this.ccseq_generalledgernumber,
"ccseq_groupcode": this.ccseq_groupcode,
"ccseq_mastercardposteddate": this.ccseq_mastercardposteddate,
"ccseq_mastercardtransactiondate": this.ccseq_mastercardtransactiondate,
"ccseq_mastercardtransactionparentcompany": this.ccseq_mastercardtransactionparentcompany,
"ccseq_navcompanycode": this.ccseq_navcompanycode,
"ccseq_navemployeeid": this.ccseq_navemployeeid,
"ccseq_navgeographycode": this.ccseq_navgeographycode,
"ccseq_navjobclasscode": this.ccseq_navjobclasscode,
"ccseq_navservicecode": this.ccseq_navservicecode,
"[email protected]": this.expenseTransactionSet + "(" + this.ccseq_setid + ")",
"ccseq_transactionamount": this.ccseq_transactionamount,
"ccseq_transactiondate": this.ccseq_transactiondate,
"ccseq_transactiondescription": this.ccseq_transactiondescription,
"ccseq_transactiontype": this.ccseq_transactiontype,
"ccseq_vendor": this.ccseq_vendor,
"statecode": this.statecode
};
return json;
}
As stated in the error, you should POST a JSON object, not an array.
I believe the change you need is to change:
toJSON(): any[] {
let json = [];
json[0] = {
//...
to
toJSON(): any[] {
let json = {
//...