last day (15 days later) » 

12:51
1
Q: Why isn't the input valid as a JSON-string C#?

Abed G.I am working on an ASP.NET application and I am sending a request (REST API) in my class library. I have a test application (Console Application) where the same code works perfectly without any error but goes wrong in the ASP.NET application of our customer. I do the following in the test applic...

My guess would be Object2 should be called Object1, so MainObject is an array of Object1
Where are all of those \"s coming from? Is that just how it's being displayed or are they actually in the JSON?
@ThomasKoelle this being parsed, is exactly what the service I am connecting to expects. And it works perfectly fine in my test application, very strange.
@KirkLarkin That is what Visual Studio shows when inspecting a JSON string. But that is not an issue.
According to the spec, JSON doesn't allow trailing commas in array and object notations. Your objects have each a trailing comma.
@AbedG.: It absolutely is an issue, in terms of the question. If the backslashes are being shown in the debugger, they aren't in the actual string. It's really important that we know the exact content of the string. Please print it out to the console, then copy that into the question.
12:51
If that is what is being shown when printing it to the console, like @JonSkeet says to do, then those backslashes is an issue, let's avoid guessing what the string actually contains.
Note that some things, like trailing commas, and /* comments */, are ignored by some libraries, like Json.Net, when parsing json, but it is still invalid json syntax, so not all libraries will ignore that and instead fail parsing the (invalid) json. This might be why you're getting different behavior.
Trailing commas in a Json file/body is also a strong signal that the json is hand-crafted, probably using something like razor pages or other templating, or even just string concatenating. While many libraries will ignore these kinds of things during parsing, I know of no libraries that actually adds trailing commas.
@JonSkeet I have added the full JSON, but replaced the name of the properties and some values of course. However, the string JSON object in my question is how it looks like when using JsonConvert.SerializeObject();
@AbedG.: You've still shown a string with backslashes in. If that's genuinely the content of the string, that's entirely different to if the backslashes aren't there. It's really important that we know the actual content of the string, not just what the debugger shows.
Now you've removed the backslashes - but there are no trailing commas, which you were showing before. Are the trailing commas there or not?
@JonSkeet I have updated the JSON content. This is what Console.WriteLine(); prints out.
@JonSkeet This is exactly what is being printed out in the Console.WriteLine(); The last property and object show no trailing commas.
I deleted my answer since it doesn't actually apply, apparently. This is actually looking very much like valid JSON.
@Pac0 No problem. I appreciate your will to help.
12:51
Okay, so that's now valid JSON. Could you give more context in the question about the difference between your setup and your customer's? You talk about a library making a request, and also an ASP.NET server - is the library used within the server, or is it making requests to the server? Is the ASP.NET server that the customer's running executing your application? If so, what's making a call to it? (If it's some 3rd party making invalid requests, there's not much you can do about that.) Just clarifying the first paragraph would help.
@JonSkeet There is really no difference between the core code in my test application and the customer's ASP.NET solution. I am running both codes locally, and the request is being made to a Salesforce service from my locally run code. The same code in my own written console application works perfectly fine. But running the same piece of code in the ASP.NET application (also locally) tells me the JSON-string input is invalid.
@JonSkeet I have the Newtonsoft.Json v11.0.0.0 in both solutions, that might not be the problem. However, I find it very weird that the same request and code works in the test console application, but goes wrong in the ASP.NET application (run in a class library).
It's definitely time to isolate this. First, take the Salesforce service out of the picture if possible. Log the request you'd make from your working code, and the same thing from the broken code. If it looks like the broken code is creating invalid JSON, that's one thing... but if it looks like the two requests have the same JSON, then presumably it's some other aspect of the request. You've been focusing on the JSON, but it could be authentication or something similar, just with a poor error message from the server.
(And please update the question as well - the question implies that the failure only occurs at the customer site, whereas it sounds like you can reproduce it locally.)
@JonSkeet Your last sentence "could be authentication or something similar, just with a poor error message from the server" is exactly what it is. I found out what the problem is. It has nothing to do the with the core functionality, it does all work. It is the server that gives me a very poor error message. I have tested it, and it is one of the properties that causes an error on the SF service. Thank you for your time Jon!

last day (15 days later) »