JSON object requires curly braces? - json

I have an JSF application that stores/retrieves data to/from Oracle DB. Very simple application. I used Gson (from google) API to convert String type data to JSON format into a table in DB.
When I inspected stored data from the table, I expected "{" and "}" beginning and end but I didn't see it instead data was wrapped with double quotes. Is this correct?
Here is an example.
From UI (.xhtml) there is an inputText field to capture data. The data is part (property) of JPA entity model and this model is persisted to DB.
(foo = foo)
gson.toJson(variable_that_holds_above_value);
After querying data from the DB, data stored as ... The column that contains below data is clob type.
"(foo \u003d foo)"
"=" is encoded as "\u003d" b/c I didn't use disableHtmlEscaping().
[update]
I realized that gson.toJson() does what it should but what I wanted/expected was "{"keyName":"foo=foo"}. I naively thought simply providing a string to toJson() will do that for me.
I decided to use JsonObject instead. Reading this How to convert a String to JsonObject using gson library helped me.
JsonObject jb = new JsonObject();
jb.addProperty("keyName","foo=foo");

Related

Create list of custom objects from flat JSON data

I want to create an arraylist of type Adapter from a JSON. But since the JSON is not in arraylist format, I'm unable to use gson.fromJson() method.
Is there any way by which I can create a list of my custom object by parsing the following JSON?
JSON data:
"source":{"adapter-config.adapter[0].name":"testAdapter1",
"adapter-config.adapter[0].resolverName":"serviceResolver",
"adapter-config.adapter[0].parameters[0].key":"serviceId",
"adapter-config.adapter[0].parameters[0].value":"serviceIdPathInEvent",
"adapter-config.adapter[0].parameters[1].key":"appId",
"adapter-config.adapter[0].parameters[1].value":"appIdPathEvent",
"adapter-config.adapter[0].parameters[2].key":"env",
"adapter-config.adapter[0].parameters[2].value":"envPathInEvnet"}
My Adapter Object:
public class Adapter {
private String name;
private String resolverName;
private List<KeyValuePair<String, String>> attributeList;
}
Gson does not provide such functionality out of the box. However you can achieve this by manually reading the JSON data from a JsonReader, consuming the JSON property names with nextName() and then parsing them to determine which data they represent. You could either directly read from a JsonReader, or in case the shown JSON data is only an extract from a larger JSON document, you can implement a TypeAdapter for your List<Adapter>. That TypeAdapter could then either be registered with a GsonBuilder by providing new TypeToken<List<Adapter>>() {}.getType() as type, or you could annotate the field holding the List<Adapter> with #JsonAdapter.
For the actual parsing of List<Adapter>, I would recommend storing a current adapter (and its index in the list) in a local variable. Whenever you parse a JSON property name, you could then check if the index encoded in the name is equal to the index of the current adapter, then you are going to modify the existing instance, otherwise if the encoded index is equal to the index of the current adapter + 1 you create a new Adapter instance, add it to the list of adapters and reassign the current adapter variable and its index variable. Then you continue with parsing the remainder of the property name to find out which Adapter field values to set.
(In case you get stuck there, feel free to let me know in the comments and I can try to provide some concrete code; but it would probably be best if you tried it yourself first.)

Is it possible to write json data to a file in azure blob storage without converting it to string?

I'm working on a project in azure databricks where I need to write my transformed data which is in JSON format to a file(.json) which further is written to DB.
I've tried with dataframes,rdd options. some snippets of the things I've tried
df.collect.map( line => {
//transformation logic to create json
(field1,field2,json);
})
var dataframe = processedList.toList.toDF("f1","f2","json");
dataframe .repartition(1).write.mode("overwrite").json(path)
This code works fine but the 'value' which is json data is treated/written as String as it contains all the escape characters etc. Cannot directly use JsonObject as dataframe doesn't support it.
So is there a way to write to the file without it converting to String?
What's the type of json column? It's probably string, thus Spark treats it as a string literal. Try
df.withColumn(to_json("json").alias("json")).write.json(path)

Deserialize a string to json

I want to know whether deserialize converts json to string or string to json.I need my string to be returned as Json so i used deserialize, but unsure about its syntax.Can anyone direct me correctly.
My code
JavaScriptSerializer datajson = new JavaScriptSerializer();
var objec = datajson.Deserialize<string>(data);
return Json(objec,JsonRequestBehavior.AllowGet);
Serialisation is the act of taking objects and turning them into something more persistable or communicable, i.e. turning objects into JSON, XML or binary data.
Deserialisation is the act of taking serialised data and turning it back into objects.
So in your case, if you want to turn your objects/variables into JSON, the process is called serialisation.
Your code, assuming it is MVC C# (you may wish to add these tags to your original post), appears to be deserialising a JSON encoded string into a string, then serialising it back to JSON again when it returns the view. I'm not sure why you would want to do this. You should be able to simply do:
return Json(data, JsonRequestBehavior.AllowGet);

Can Hibernate automatically map a HashMap to a string column?

I have a HashMap which was populated from form elements of an HTML page, and when I save it I need Hibernate to convert it automatically to a JSON string and also persist it as a JSON string. Is this doable in Hibernate? Or, can you tell Hibernate in the mapping file (or maybe, as an annotation) to call a Java method that converts HashMap to JSON, and persist the return value of that method, and vice-versa? (read JSON string and convert automatically to a HashMap).
Thanks for all the help!
Sure, you can just create a custom user type:
http://docs.jboss.org/hibernate/orm/3.6/reference/en-US/html/types.html#types-custom
Hibernate will then use this to persist data to the database, and to read it back as object.

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.