How automatic escape quotes in json (C#) - c#

From server I get json. Json is very big. I show litle piece of this
{
"id": "9429531978965160",
"name": "Morning in "Paris"", // json.net cannot deserialize this line, because line have no escaped quotes.
"alias": "ThisAlias"
}

The problem is the server side that generates invalid JSON.
You could try writing a regex that fixes this (searches for any quotes in between the third and last). Just note that there might be many other issues with the JSON, like newlines that are not escaped etc.

It's not just that the output you are receiving is non-standard json, it's broken in such a way that it's not a well-defined language and doesn't parse unambiguously even in the simple cases. How should you parse {"a": "A", "b": "B"}? One way is as legal json. Another valid parse is a single property a with the value "A\", \"b\": \"B".
As others have said, the best resolution is to fix the server so that it no longer outputs invalid garbage. If that's not an option, you'll have to write your own parser. A normal parser would declare an syntax error at the 'P' in "Paris". Your parser could back up to the last quote token and try to treat it as if it were escaped. The next syntax error is at the second of the consecutive quotes, and again it could back up and treat the quote token as if it were escaped. If there are any other ways in which the input deviates from legal json you'll need to handle those as well.
If you're not familiar with parsers, this will take a while. And when you're done you'll have a parser that recognizes a poorly-specified and almost totally useless language, which is to say that it will largely be a waste of time. Do what you can to fix it on the server side.

Related

Reliably fix broken escape sequences in JSON

I'm getting some JSON for an outside source that can't be changed and apparently they don't understand the rules about escaping characters correctly in JSON string values. So they have a string value that might have tabs in it, for example, that should have been escaped and other invalid escape sequences like \$. I'm trying to parse this with JSON.Net but it keeps falling over on these sequences.
For example, the source might look something like this:
{
"someRegularProp": 10,
"aNormalString": "foo bar etc",
"anInvalidString": "foo <tab \$100"
}
and it's parsed with
var obj = JObject.Parse(json);
So I can fix this specific case with something like:
json = json.Replace("\t", "").Replace("\\$", "$"); // note: in this case I'm fine with just stripping the tabs out
But is there a general way to fix these problems to remove invalid escape sequences before parsing? Because I don't know what other invalid sequences they might put in there?
I don't see general way. Obviously they are using bugged library or no library at all to generate this output and unless you explore more, all you can do is try as much output from them as possible to find all problems.
Perhaps make a script to generate as much output as possible and validate all of that, then you can be at least a bit more sure.

Corrupted JSON HTTP response

I am getting a HTTP request for a website and the content type is JSON. However, I am getting a nested JSON that is a unicode and is causing consistency problems.
Here is an example:
{"key1":"value",
"key2":"value",
"key3":{
u'key31':u'value',
u'key32':u'value'}}
This reminds me of python 2.7 troubles but I am not sure how to fix this JSON. I am using C# to parse it. Everything works correctly until I try to access key3.
The content should be a JSON object type but it is considered rather a value or a string.
Thanks for ya help. Is there a way to fix it if it is actually corrupted or am I parsing it wrongly?
You're correct that this json object is not complete / does not have the correct syntax. You're missing a closing '}' character.
How are you parsing your data? Try taking a look at this documentation.
your json object is not in valid formatted it should be like as folllows
{
"key1":"value",
"key2":"value",
"key3":{
" u'key31'":"u'value'",
"u'key32'":"u'value'"
}
}
by any chance do you get this json from python dump? coz Python's unicode literals are not valid JSON, and neither are single quotes

Remove trailing spaces in json string c#

I have a large json string that I need to remove any number of leading and trailing spaces from property values (in c#) e.g.
"Some Property Name": " Some Value "
needs to change to:-
"Some Property Name": "Some Value"
I have the option to do this via a regex replace on the json string before it is converted into a newtonsoft json object, or loop through the json object's properties after it has been converted.
Anybody any thoughts on the best way to do this?
Your second option is the safest one.
Any time that you have to modify a structured text of some kind (XML, HTML, JSON, C#, etc.) the safest option is to parse, modify, and re-format. Otherwise, you run the risk of changing things that you did not plan to change.
In your particular scenario a regex solution may unintentionally strip leading spaces from quoted strings inside a string, for example
"Some Property Name": " Say \" Hello, world!\" two times "
Corner cases like this often go unnoticed when developing a regex-based solution. On the other hand, parser-based solutions do not treat these situations as "corner cases," because all the complexity of understanding the format is shifted into the parser.

String.Format not taking 4th object

Here is my prob, I wanted String.Format() function should take 4 objects and format string. But it throws "Input string not in a correct format error".
Here is my code,
string jsonData = string.Format("{{\"sectionTitle\":\"{0}\",\"strPushMsg\":\"{1}\",\"Language\":\"{2}\",}\",\"articleid\":\"{3}\"}}", urlsectiontitle, formatHeadline, Language, articleid);
\"{2}\",}\"
Looks like you need to escape that closing brace by doubling it:
string.Format("{{\"sectionTitle\":\"{0}\",\"strPushMsg\":\"{1}\",\"Language\":\"{2}\",}}\",\"articleid\":\"{3}\"}}", urlsectiontitle, formatHeadline, Language, articleid);
It appears you are creating JSON. This can use single quotes (which would avoid all the escaping), but even better use a tool like JSON.Net designed to create JSON. While your (partial) structure here is quite small (the unmatched } shows this is only partial), and the JSON gets bigger it is much easier to use a tool to get it right.

Posting JSON with '\/' gets escaped as '\\/'

I'm currently facing a problem when posting the following JSON Object:
{
"event": "orders.updated",
"address": "http:\/\/82.75.163.12\/BiedMeerIntegration?connectionId=545",
"is_active": true
}
The problem with the above is that .NET escapes all backslash characters and posts it like:
{
"event": "orders.updated",
"address": "http:\\/\\/82.75.163.12\\/BiedMeerIntegration?connectionId=545",
"is_active": true
}
I tried different libraries (like ServiceStack and the Microsoft HttpClient) but they all result in the same error. I don't have control of the REST interface and it specifically accepts the url format stated in the first JSON sample.
I also had a look at the GenericUriParser options in .NET 4 but it seems it doesn't make a difference: http://msdn.microsoft.com/en-us/library/ee656542.aspx
How will I be able to format the request correctly?
The problem is your string:
{
"event": "orders.updated",
"address": "http:\/\/82.75.163.12\/BiedMeerIntegration?connectionId=545",
"is_active": true
}
Is actually already escaped, and in fact is perfectly correct JSON. Note that, both in JavaScript and JSON, escaping the / is optional and only required when such symbol comes after <, in order to prevent unwanted closing of a <script> tag.
The output of your JSON serializer should already look like that (even if those \/ are not really needed, they are correct). So I don't really understand why you are giving a fragment of already-well-formed JSON to a JSON serializer... which then escapes it again. If you already have correct JSON, shouldn't you simply write it to the output stream?
On the contrary, if what you have is the "http:\/\/82.75.163.12\/BiedMeerIntegration?connectionId=545" string coming from somewhere in your application's model or services (and not the already-formed JSON fragment as you show it to us), then the problem is such string is already escaped -in JavaScript or JSON, can be any-, and you should first correctly unescape it in its corresponding language before giving it to the serializer. But don't unescape the JSON fragment above as a whole!... a fragment of JSON without adequate escaping makes no sense, would be simply not JSON.

Categories