I am retrieving JSON from an API and have the following problem:
Some Json-values look like this and cannot be serialized the standard way
"key": "This is just a "dummy" value to show the problem",
The problem are the quotes around dummy. Newtonsoft obviously thinks the value ends with the quote before dummy, but it actually ends after problem.
Is there a way to ignore those quotes or somehow remove them automatically?
I've tried to remove them with a StringBuilder and String-Replace, but that didn't work because such a pattern occures mutliple times in the JSON-File and sometimes the nested quotes quote a single word, sometimes a whole sentence.
The whole JSON from the API has around 50.000 lines, so it's impossible to correct the error by hand.
Can this be solved somehow in C#?
Update: You have to write a custom parser to parse since its clearly not JSON then. What you have to do is fix the serialized object before you deseriliaze it. You have to iterate through the entire string and remove the unrequired quotes.
An example would be when the value property of a JSON ends and next one begins there is a comma character in the middle.
Its basically a huge nested if condition, to fix this.
Original Answer
As you can see, it does not parse as an valid JSON. You have to represent quotes as below. If its not someting in your control, you have to come up with a custom parser.
Related
I have a string in plain text which contains brackets like JSON format as it is created using JavaScriptSerializer().Serialize() method. I need to remove brackets and collon and want to convert it into key = value, key = value format.
Need to convert
{
"account":"rf750",
"type":null,
"amount":"31",
"auth_type":"5",
"balance":"2.95",
"card":"re0724"
}
to
'account=rf750,type=null,amount=31,authe=5,balanc=2.95,card=re0724'
Well, you've got three different things going on here.
The first, and surface issue, is: how do you change the string?
Simple - you do some string substitutions, preferably using Regex. Remove the starting/ending braces, change [a]:"[b]", to [a]=[b], - or however you want the final format to look like.
The second, and slightly deeper issue is: JSON isn't just a simple list of keys=values. You can have nesting. You can have non-string data. Simply saying you want to change the JSON result to key=value,key=value,key=value, etc - is fragile. How do you know the JSON structure will be what you're expecting? JSON Serialization will serialize successfully even if you've got nested structures, non string/int data, etc. And if you want solid code that doesn't easily break, you have to figure out: how do I handle this? Can I handle this?
The third, and final thing is: you're taking a standard data format schema and figuring out how to translate it to a nonstandard data format. 90% of the time someone does that, they deserve to be shot. Seriously, spend some solid time asking yourself whether you can use the JSON as-is, and whether the process wanting key=value,key=value,etc can be changed to use an actual standardized data format.
Here is simple solution which (1) parses json to Dictionary and (2) uses String.Join and Linq Select to provide desired output:
using System.Linq;
using Newtonsoft.Json;
..
var dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
var str = string.Join(',', dict.Select(r => $"{r.Key}={r.Value}"));
str-variable now contains:
account=rf750,type=,amount=31,auth_type=5,balance=2.95,card=re0724
Well thanks everyone for your time and response. your answer led me towards solution and finally i found the following solution which resolved the issue perfectly.
var jObj = (JObject)JsonConvert.DeserializeObject(modelString);
modelString = String.Join("&",jObj.Children().Cast<JProperty>().Select(jp => jp.Name + "="+ HttpUtility.UrlEncode(jp.Value.ToString())));
the above code converts the JSON into a url encoded string and remove the JSON format
I'm using the SendGrid Api and apparently their API does not like an array of email messages to be passed in. It wants an array of objects without the outer brackets.
In their example
http://www.newtonsoft.com/json/help/html/SerializingCollections.htm
You can see that the result of serializing a collection results in a Json Array of objects which makes perfect sense. But is there a way to omit the outer brackets or will I just have to parse them out of the string manually? I'd rather avoid parsing if possible.
Thanks.
I doubt you'll find a serializer that does that because the behavior you're describing isn't valid JSON (see the format specification at http://www.json.org/), so by definition a correctly-implemented serializer won't do this. It's a little unclear to me exactly why you'd want to do this in the first place and I don't recommend doing it but if you must you can always manually strip out the brackets using a regex or something.
I have some JSON being sent to me that breaks when it is trying to be deserialized. It seems to contain a black diamond with a ? in it. I cannot see the character but it is obviously there and it is failing on my system.
How do I get rid of this and still leave my JSON intact for deserialization?
UPDATE:
Here is a example of what will be in the middle of my JSON:
"UDF5" : "�65",
I am even open to just removing this property from my JSON altogether via RegEx.
As answered for: remove piece of string (JSON string ) with regex and based on the formatting you provide in that question (and I am assuming will edit into this one):
Assuming I can rely on the formatting you show above and it is one of these per regex being run this can be accomplished as simply as something like
([\S\s]*\"])\"UDF5\" : \"[\S\s]*?\",([\S\s]*)
Using the back reference $1$2 referencing the parts before and after the UDF5 field to write back out.
If there is a newline there to remove I am not doing it right now. This could be better - if someone else has time to correct or provide an additional answer. But in the interests of getting you an emergency fix I hope this helps.
Trying: T obj = JsonSerializer.DeserializeFromString<T>(jsonData);
on a string that has several \n's throughout it. JayRock's library successfully deserializes this like: T obj = (T)JsonConvert.Import(typeof(T), jsonData);
Is this a bug, or do I need to manually strip out newlines?
The problem I ran into wasn't the \n's, but instead was the lack of public properties on my DTO's.
RE: Can ServiceStack.Text deserialize JSON to a custom generic type?
The debugger preview popup shows actual linebreaks as \n so that the preview remains single line. The text visualizer shows linebreaks correctly.
This implies to me that the JSON itself is broken, because newlines should be encoded with \n.
Linebreaks in strings are illegal in Javascript, and thus also in JSON.
If this doesn't happen to the the issue: the nuget version was published 1st Oct, but there's a commit in github dated 3rd Oct with comment "fix whitespace issues hopefully once and for all". Worth trying.
I'm having a really strange issue serializing to an MSMQ and back.
The object being serialized contains a string array; one of the strings in the array contains spaces and carriage returns ("\r\n"). The object is constructed fine and seems to serialize without a problem, but when I deserialize it (in another project), the array now contains an item for every individual word and space.
i.e.:
the array { "first", "this is a test string" }
becomes { "first", "this", "", "is", "", "a", "", "test, "", "string" }
I have no idea what's going on... as far as the serialization, I'm using MessageQueue.Send() and System.XML.Serialization.
Any help?
\r\n seem to be messing up your serialization. Can you intercept the serialization and replace them with another set of characters before dehydrating them and then adding them back when re-hydrating?
What does the serialized xml look like? If you look at the actual xml output, you should be able to tell whether it is the serializer or deserializer that is causing the problem, which would certainly help in attempting a fix.
With that said, i will say that i very often serialize objects that contain the exact formation of data you are specifying and i am fairly certain i've never seen this behavior.
Perhaps a code example to repro the issue would be helpful.