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

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.

Related

Convert inconsistently formatted JSON String to Object

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);

Convert doctrine array to JSON

Is there a way to read a column of doctrine type "simply_array" or "array" in json?
My doctrine database is approached from another api and I want to read data from that api. However there is a column of type doctrine array that I want to convert into JSON.
I am unsure if there is a preferred way of doing this or I need to hack my way around it.
Here is an example of what is stored in the database as a doctrine array:
"a:1:{i:0;a:3:{s:3:\u0022day\u0022;i:5;s:4:\u0022time\u0022;s:7:\u0022morning\u0022;s:12:\u0022availability\u0022;N;}}"
That looks like the format of PHP's serialize() function. And the literal double-quotes in the string have been converted to unicode escape sequences.
You could do the following:
Fetch the serialized string
Fix the \u0022 sequences (replace them with ")
unserialize() it to reproduce the array
Convert the array to JSON with json_encode().

Json : getting the unwanted result

I'm using json plugin to get the response in json.
But I m getting the unwanted result:
Here is what I get:
{"data":"[[\"service\",\"webservices\",\"document\"],[\"validation\",\"adapters\",\"server\"]]","records":25,"recordsTotal":75}
originally the data var in my action class is like this:
[["service","webservices","document"],["validation","adapters","server"]]
but json plugin adds the backslash.
The wanted result is that:
{"data":[["service","webservices","document"],["validation","adapters","server"]],"records":25,"recordsTotal":75}
Is there a way to get the later result ?
Thanks
You're representing the data as a PHP string. " is obviously a reserved character in JSON, so your serialization library is dutifully escaping the quote using /.
If you set up the PHP variable so it's an array of arrays, instead of a string representing an array of arrays, then your JSON serialization will work fine.

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 formated String to JsonObject with Jayrock

I have a request parameter in my ASP.NET app. that is in JSON format, and I was wondering if there is a good (quick and easy) way to convert a JSON string to a Jayrocks JsonObject, so I can easily extract key-value pairs without the need to manually parse the string?
Assuming json is the variable containing JSON text, use Jayrock.Json.Conversion.JsonConvert.Import(json). What you will get back in return is either a JsonObject, JsonArray, JsonNumber, System.String, System.Boolean or a null reference depending on the root JSON value in the source JSON text. If you know it is going to be a JSON object for sure then you can safely cast the return value or use JsonConvert.Import<JsonObject>(json).
I would discourage working against JsonObject directly unless you particularly depend on one of its features. You should just pretend the JSON object you get back is a dictionary; either IDictionary or IDictionary<string, object>. With the latest version for .NET Framework 4, you can also work with a JsonObject as a dynamic object.
I don't know Jayrock, but if you want to accept a JSON object as a parameter of Action in MVC2 than the easiest way to do it is by using JsonValueProviderFactory from Futures assembly.
It's part of System.Web.Mvc in MVC3.