As you have noted, the JSON you are passing is not correct (additional curly braces). Once you fix that, JObject
will get populated. Right now it should be null?
Sending json through api post request
I'm currently struggling to send a JSON via post request into my controller to use it further by calling a stored procedure.
Here is the post request using axios:
async returnJSON(model) {
this.info = await axios.post(
"https://localhost:44349/api/items/AddItem/",model)
.then((response) => response.data);
Here is the structure of model
{
"arrayAutori": [
{
"dinUniversitate": true,
"creatorType": 4,
"creatorID": 3
}
],
"itemID": "",
"itemTypeID": 6,
"drepturiDeAutor": {
"valueID": "",
"fieldID": 15
},
"isbn": {
"valueID": "",
"fieldID": 25
},
"limba": {
"valueID": "",
"fieldID": 7
},
"numarPagini": {
"valueID": "",
"fieldID": 43
},
"data": {
"valueID": "",
"fieldID": 6
},
"editura": {
"valueID": "",
"fieldID": 23
},
"editie": {
"valueID": "",
"fieldID": 42
},
"volum": {
"valueID": "",
"fieldID": 19
},
"numarColectie": {
"valueID": "",
"fieldID": 41
},
"titlu": {
"valueID": "",
"fieldID": 1
}
}
And here is my controller:
public string AddItem( JObject jsonString)
{
db.Database.ExecuteSqlCommand("exec zotero.PublicationsMerge " + jsonString);
db.SaveChanges();
return "1";
}
I can not make a model class to use it, because that JSON may vary depending on the publications. Also, when I'm debugging this controller, jsonString looks like this:
{{
"arrayAutori": [
{
"dinUniversitate": true,
"creatorType": 4,
"creatorID": 3
}
],
"itemID": "",
"itemTypeID": 6,
"drepturiDeAutor": {
"valueID": "",
"fieldID": 15
},
"isbn": {
"valueID": "",
"fieldID": 25
},
"limba": {
"valueID": "",
"fieldID": 7
},
"numarPagini": {
"valueID": "",
"fieldID": 43
},
"data": {
"valueID": "",
"fieldID": 6
},
"editura": {
"valueID": "",
"fieldID": 23
},
"editie": {
"valueID": "",
"fieldID": 42
},
"volum": {
"valueID": "",
"fieldID": 19
},
"numarColectie": {
"valueID": "",
"fieldID": 41
},
"titlu": {
"valueID": "",
"fieldID": 1
}
}}
I don't know why, but it adds another pair of {}. What am I doing wrong?
2 answers
Sort by: Most helpful
-
Jaliya Udagedara 2,821 Reputation points MVP
2022-05-01T13:56:41.793+00:00 -
Bruce (SqlWork.com) 68,311 Reputation points
2022-05-01T16:54:10.223+00:00 Even if the json string is valid, the following
public string AddItem( JObject jsonString)
{
db.Database.ExecuteSqlCommand("exec zotero.PublicationsMerge " + jsonString);
db.SaveChanges();
return "1";
}Won’t work because it’s not valid sql. Sql does not support json literals. Maybe the sp is expecting a string variable, that is parsed. Pass the json value a string parameter .