I have an SQL Server proc that returns JSON. I know the proc is working and returning valid JSON, as I can test it with various tools including the SQL function isjson.
However, some tools when calling my API that calls these procs, are unable to verify the JSON as it's getting wrapped in double quotes and escaping every double quote in the JSON.
This is the SQL output:
{"APIResult":[{"ID":200,"Status_Message":"Success","Developer_Message":"Successful login for D56D10AC-3A74-42FC-8BB5-F783A6C0C556 33E4F907-F1E5-4F1B-8CA5-5521291E683A (AppID: (null)).","User_Message":"Successful login","User":[{"ID":"D56D10AC-3A74-42FC-8BB5-F783A6C0C556"}]}]}
And this is the output from postman:
"{\"APIResult\":[{\"ID\":200,\"Status_Message\":\"Success\",\"Developer_Message\":\"Successful login for D56D10AC-3A74-42FC-8BB5-F783A6C0C556 33E4F907-F1E5-4F1B-8CA5-5521291E683A (AppID: (null)).\",\"User_Message\":\"Successful login\",\"User\":[{\"ID\":\"D56D10AC-3A74-42FC-8BB5-F783A6C0C556\"}]}]}"
Some parsing tools (jsonlint) are fine with the second format and return it as valid JSON. Others (http://json.parser.online.fr/), not so much. Postman is obviously happy, but my mobile developer is unable to get it to work as valid JSON.
After some help here, I understand that the API "should" be outputting a header content-type of application/JSON and the output should not be a string.
The controller has:
public string Get(string key) {
string message = "";
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(cmd2);
da.Fill(ds);
message = ds.Tables[0].Rows[0][0].ToString();
return message;
}
Which kind of implies it is being converted to a string.
So my question is:
How do a set the header on the output, making it return JSON not a string?
The data is most likely being double serialized. This the escape characters in the actual response.
since the data from the database is already a JSON string then return the content as is, but the actions would need to be refactored a bit.
The raw JSON is passed as
StringContent
directly to the response to avoid the framework trying to serialize it for you.