Convert inconsistently formatted JSON String to Object - json

I'm having the below JSON coming in as a String input to my code. Since the string isn't uniformly formatted, overcoming the escape characters and grouping of the quotes to read the string and convert it into Java Object and sub-objects has run into issues
{"payload":{"details":"{\"source\":\"incor\",\"type\":\"build\",\"created\":\"1553855543108\",\"organization\":null,\"project\":null,\"application\":null,\"_content_id\":null,\"attributes\":null,\"requestHeaders\":{}}","content":"{\"project\":{\"name\":\"spinner\",\"lastBuild\":{\"building\":false,\"number\":0}},\"master\":\"IncorHealthCheck\"}","rawContent":null,"eventId":"bb357b79-069b-426d-8d21-8d04b06f5009"},"eventName":"city_spinner_events"}
I've tried using GSON, Jackson so far to try and read the String and convert into object and sub-objects. However, I've been able to objectify only the top level object. I face issues while I need to create sub-objects due to the escape characters and misreading of grouping of quotes by the parser. It throws errors and exceptions.
The expected JSON is as below which can be converted to object :
{"payload":{"details":{"source":"incor","type":"build","created":"1553855543108","organization":null,"project":null,"application":null,"_content_id":null,"attributes":null,"requestHeaders":{}},"content":{"project":{"name":"spinner","lastBuild":{"building":false,"number":0}},"master":"IncorHealthCheck"},"rawContent":null,"eventId":"bb357b79-069b-426d-8d21-8d04b06f5009"},"eventName":"city_spinner_events"}

Try unescapeJava from org.apache.commons.text.StringEscapeUtils,
StringEscapeUtils.unescapeJava(str);

Related

Swift Json Single Quote Parse

I have a problem while parsing json which includes single quote. I am using JSONDecoder. I added response from API at below and I don't want to do any replacement or some regex operations. Are there any workaround for that?
"{\'value1\': true, \'value2\':\'2021-02-08\'}"
Your string is simply not valid JSON. Since it's not valid JSON there's no way to configure JSONDecoder to decode it.
If you aren't in charge of the service that outputs that string, the only thing you can do to use it with JSONDecoder is to modify the string to become valid JSON by, as you suggested, doing text replacement.

Converting Delphi Objects to JSON

I am using Delphi XE7 and I am having trouble converting objects into JSON. I can get some object to give back what I think is proper JSON, eg TTestObject:
{"Test":{"Field":"TestField","Operation":"TestOperation","values":
["Value1","Value2","Value3","Value4"]}}
JOBJ:= TJSONObject.Create;
JOBJ.AddPair('Test', ATestObject.JSONObj);
memo1.Lines.Add(JObj.ToJSON);
JOBJ.Free;
However, when I try to get JSON back from my objects that have properties that are objects as well, I get JSON with \ characters.
{"Exceptions":{"TestObject1":"
{\"Mode\":\"0\",\"Value\":\"100.50\",\"Days\":\"10\"}","TestObject2":"
{\"Mode\":\"0\",\"Days\":\"0\",\"UnitsSold\":\"
...
What is causing this?
The JSON is perfectly valid. Your nested objects, when represented as JSON, contain double quote characters. Since they are reserved as string delimiters they need to be escaped. Hence the use of the backslash character as the escape character.

Convert json String to object when square brackets are missing in java

I am trying to parse a json string mentioned below,
"inventoryItems":{"fare":"299.00","ladiesSeat":"false","passenger":{"address":"xxx,Bangalore","age":"26","email":"xxxxxxxxx#gmial.com","gender":"MALE","idNumber":"123ABC","idType":"PAN_CARD","mobile":"9999999999","name":"abcd","primary":"true","title":"Mr"},"seatName":"27"}
The structure is there is an inventoryItems which is an array and inside that there is an passenger *array*, The problem is there are no square braces, since the array contains only one value in both inventoryItems and passenger. This malformed data is from a third party server so I cant correct them. While converting this to Object Jackson library is throwing an excception.
My question is how to form a proper json string with square braces from the above so that jackson is able to convert it into java object?
The DeserializationFeature ACCEPT_SINGLE_VALUE_AS_ARRAY should solve your problem (via [ObjectMapper.enable(...)) without a conversion of the string.

Json object with backslash, gson serialization

I want to convert a java object to json object with string format*. I am using gson library. Is there any way to do that.
(I am not sure if it is the correct name for this structure) json object with string format:
*
{ [\"name\":\"Ajay\",\"age\":30,\"email\":\"ajay#ajay.com\"]}
I'm pretty sure gson itself can't handle this, but you can. Given string s looking like
{ [\"name\":\"Ajay\",\"age\":30,\"email\":\"ajay#ajay.com\"]}
you only need to call gson on s.replace("\\\"", "\""). Simply clean up you string, so it looks like it should (your quotation marks look differently, maybe you need to fix it, too).
This may be too abstruse, but if you create a JSON string (through GSON or another library like JSON.org) and GSON-serialize that string, you will get the backslashes. This has been an irritation for me, but it would work for your case, with more code than the replace but safer if backslashes are otherwise valid in your JSON.

How to convert string to BSON using MongoDB C++ driver?

Similar to toString is there a way we can convert a string to BSON object? I need to remove a document using C++ driver the the remove function expects the query to have BSON object.
Use the fromjson method found here:
http://api.mongodb.org/cplusplus/1.5.4/namespacemongo.html#a4f542be0d0f9bad2d8cb32c3436026c2
BSONObj mongo::fromjson ( const string & str )
Create a BSONObj from a JSON <http://www.json.org> string.
In addition to the JSON extensions extensions described here
http://mongodb.onconfluence.com/display/DOCS/Mongo+Extended+JSON, this function accepts
certain unquoted field names and allows single quotes to optionally be used when
specifying field names and string values instead of double quotes. JSON unicode escape
sequences (of the form ) are converted to utf8.
Exceptions:
MsgAssertionException if parsing fails. The message included with this assertion includes
a rough indication of where parsing failed.