Retrieve value from database and Serialize into json - java

The request json looks like this:
{
"key1": "value1",
"generic": {
"key1":"string-type-value1"
"key2":"string-type-value2"
"key3":"complex-type-value3"
.
..
...
"keyn": "simple/complex-valuen"
}
}
The value of "generic" attribute above is a complex json.
I'm able to persist the entire complex json in a database column (by deserializing the json into a byte array and then persisting the value in the database as a blob type column).
However, when I try to retrieve the data again from the database, it's giving me a JSON string instead of a complex json
How can I re-create the exact complex json back from database using Jackson library?
I don't need a string representation of the json, but I need the original json back.

Would a JsonNode instance be fine?
final String json = "" +
"{" +
" \"key1\": \"value1\"," +
" \"generic\": {" +
" \"key1\":\"string-type-value1\"," +
" \"key2\":\"string-type-value2\"," +
" \"key3\":\"complex-type-value3\"," +
" \"keyn\": \"simple/complex-valuen\"" +
" }" +
"}";
final JsonNode node = mapper.readValue(json, JsonNode.class);
System.out.println(node.get("generic").get("key1")); // "string-type-value1"

Related

Translating json to string variable

I'm working on developping a webservice that communicate two application with each other, the first application will send a json object to the second one.
I'm stuck in translating this json object :
$body = "{"fields":{"project":{"key":"'+$projectKey+'"}
,"issuetype":{"name": "'+$issueType+'"}
,"summary":"'+$summary+'"
,"description":"'+$description+'"
,"customfield_12721":"'+$FirstName+'"
,"customfield_12722":"'+$LastName+'"
,"customfield_12723":{"value":"'+$EmployeeCategory+'"}
,"customfield_12732":"'+$Externalfunction+'"
,"customfield_12725":"'+$CorporateID+'"
,"customfield_12726":{"value":"'+$VermegCompany+'"}
,"customfield_12685":{"value":"'+$IndusRegion+'"}
,"customfield_12673":{"value":"'+$Product+'"}
,"customfield_12727":{"value":"'+$Profile+'"}
,"customfield_12667":{"name":"'+$Manager+'"}
,"customfield_12708":"'+$BeginDate+'"
,"customfield_14000":"'+$Reglementation+'"
,"customfield_14001":"'+$Department+'"
,"customfield_14002":"'+$SubDepartment+'"}
}";
To a String variable like :
String json = "{fields:{project:{\"key\":\""+ projectkey +"}"
+ "\",\"issuetype\":\""
+ "\",\"customfield_12721\":\"" + employee.getFirstName()
+ "\",\"description\":\"" + description
+ "\",\"summary\":\"" + summary
+ "\",\"customfield_12722\":\""+ employee.getLastName()
+ "\",\"customfield_12732\":\"" + employee.getFte()
+ "\",\"customfield_14000\":\"" + employee.getReglementation()
+ "\",\"customfield_14001\":\"" + employee.getDepartment()
+ "\",\"customfield_14002\":\"" + employee.getSubdepartment()
+ "\",\"fulltime\":" + Math.round(Double.parseDouble(employee.getFulltime().replaceAll(",",".")))
//+ ",\"email\":\"" + employee.getEmail()
+ ",\"citizenship\":\"" + employee.getCitizenship()
+ "\",\"gnn\":\""+ employee.getGnn()
+ "\",\"company\":\"" + employee.getCompany()
+ "\",\"employeeid\":\"" + employee.getEmployeeid()
+ "\",\"customfield_12708\":\"" + employee.getStartdate()
//+ "\",\"enddate\":\"" //+ employee.getEnddate()
+ "\",\"product\":\"" + employee.getProduct()
+ "\",\"customfield_12725\":\"" + employee.getInternalnumber()
// + "\",\"employeeid\":\"" + employee.getEmployeeid()
+ "\"}}";
Can you please help ?
Here you can use JsonConvert library to get json data.bellow are the example how to use it
String json = "{\"FirstName\":\"Jack\",\"LastName\":\"Tor\"}";
var data = Newtonsoft.Json.JsonConvert.DeserializeObject(json);
Console.WriteLine(data);
Console.ReadLine();
Using a web tool
If you just want to convert it fast into a string, you can use a Json to String converter on the web.
Using a library
If you however want a good solution in you applications, a way to solve this problem would be to serialize and parse using a library such as GSON. This is of course, if you don't intend to create this conversion yourself.
GSON is quite easy to use and takes care of the translation for you. Se example below:
The sending application:
Gson gson = new Gson();
String jsonStr = gson.toJson(employee); // Serialize (from Java class to JSON string)
// Send data (jsonStr) ...
The receiving application:
// Receive data (jsonStr) ...
Gson gson = new Gson();
Employee employee = gson.fromJson(jsonStr); // Parse (from JSON string to Java class)
GSON will by default name the fields the same as the member variable names in the Java class. If you need to change the field names in the JSON string, you can use #SerializedName("newName") in front of the member variables.
Example:
class Employee {
...
#SerializedName("customfield_12721") String firstName;
...
}

how to created REPEATED type in parquet file schema with avro?

We are creating a dataflow pipeline, we will read the data from postgres and write it to a parquet file. ParquetIO.Sink allows you to write a PCollection of GenericRecord into a Parquet file (from here https://beam.apache.org/releases/javadoc/2.5.0/org/apache/beam/sdk/io/parquet/ParquetIO.html). But the parquet file schema is not like what i expected
here is my schema:
schema = new org.apache.avro.Schema.Parser().parse("{\n" +
" \"type\": \"record\",\n" +
" \"namespace\": \"com.example\",\n" +
" \"name\": \"Patterns\",\n" +
" \"fields\": [\n" +
" { \"name\": \"id\", \"type\": \"string\" },\n" +
" { \"name\": \"name\", \"type\": \"string\" },\n" +
" { \"name\": \"createdAt\", \"type\": {\"type\":\"string\",\"logicalType\":\"timestamps-millis\"} },\n" +
" { \"name\": \"updatedAt\", \"type\": {\"type\":\"string\",\"logicalType\":\"timestamps-millis\"} },\n" +
" { \"name\": \"steps\", \"type\": [\"null\",{\"type\":\"array\",\"items\":{\"type\":\"string\",\"name\":\"json\"}}] },\n" +
" ]\n" +
"}");
this is my code so far:
Pipeline p = Pipeline.create(
PipelineOptionsFactory.fromArgs(args).withValidation().create());
p.apply(JdbcIO.<GenericRecord> read()
.withDataSourceConfiguration(JdbcIO.DataSourceConfiguration.create(
"org.postgresql.Driver", "jdbc:postgresql://localhost:port/database")
.withUsername("username")
.withPassword("password"))
.withQuery("select * from table limit(10)")
.withCoder(AvroCoder.of(schema))
.withRowMapper((JdbcIO.RowMapper<GenericRecord>) resultSet -> {
GenericRecord record = new GenericData.Record(schema);
ResultSetMetaData metadata = resultSet.getMetaData();
int columnsNumber = metadata.getColumnCount();
for(int i=0; i<columnsNumber; i++) {
Object columnValue = resultSet.getObject(i+1);
if(columnValue instanceof UUID) columnValue=columnValue.toString();
if(columnValue instanceof Timestamp) columnValue=columnValue.toString();
if(columnValue instanceof PgArray) {
Object[] array = (Object[]) ((PgArray) columnValue).getArray();
List list=new ArrayList();
for (Object d : array) {
if(d instanceof PGobject) {
list.add(((PGobject) d).getValue());
}
}
columnValue = list;
}
record.put(i, columnValue);
}
return record;
}))
.apply(FileIO.<GenericRecord>write()
.via(ParquetIO.sink(schema).withCompressionCodec(CompressionCodecName.SNAPPY))
.to("something.parquet")
);
p.run();
this is what i get:
message com.example.table {
required binary id (UTF8);
required binary name (UTF8);
required binary createdAt (UTF8);
required binary updatedAt (UTF8);
optional group someArray (LIST) {
repeated binary array (UTF8);
}
}
this is what i expected:
message com.example.table {
required binary id (UTF8);
required binary name (UTF8);
required binary createdAt (UTF8);
required binary updatedAt (UTF8);
optional repeated binary someArray(UTF8);
}
please help
I did not find a way to create a repeated element from Avro that isn't in a GroupType.
The ParquetIO in Beam uses a "standard" avro conversion defined in the parquet-mr project, which is implemented here.
It appears that there are two ways to turn an Avro ARRAY field to a Parquet message -- but neither of them create what you are looking for.
Currently, the avro conversion is the only way to interact with ParquetIO at the moment. I saw this JIRA Use Beam schema in ParquetIO that extend this to Beam Rows, which might permit a different parquet message strategy.
Alternatively, you could create a JIRA feature request for ParquetIO to support thrift structures, which should allow finer control over the parquet structure.
Is it a protobuf message you used to describe the expected schema? I think what you got is correctly generated from the specified JSON schema. optional repeated does not make sense in the protobuf language specification: https://developers.google.com/protocol-buffers/docs/reference/proto2-spec
You can remove null and square bracket to generate simply repeated field and it's semantically equivalent to optional repeated (since repeated means zero or more times).

URL in JSON object as POST request Android

Please help me to send a JSON object in POST HTTP request through HttpClient, in Android.
The problem I am facing is that the JSON object having the URL is replaced by forward slash ,i.e
originally it should have the following value in JSON object
{"product":
{"featured_src":"https:\/\/example.com\/wp-content\/uploads\/2015\/06\/sidney-compressed.jpg,
"short_description":"this is a test","title":"Raiders from the North"}
}
i tried many options to keep it in the above format. But it always comes as {"featured_src":
We assume this is your input
private final static String JSON_DATA = "{"
+ " \"product\": ["
+ " {"
+ " \"featured_src\": \"https:\\/\\/example.com\\/wp-content"
+ "\\/uploads\\/2015\\/06\\/sidney-compressed.jpg\","
+ " \"short_description\": \"this is a test\","
+ " \"title\" : \"Raiders from the North\""
+ " }"
+ " ]"
+ "}";
You could use replace to do the trick.
YOUR_STRING.replace("\\", "");
Finally your method would look like this, by passing your string as parameter
private static String jsonUrlCorrector(String json_data) {
json_data = json_data.replace("\\", "");
return json_data;
}
Here is the input:
{"product":[{"featured_src":"https:\/\/example.com\/wp-content\/uploads\/2015\/06\/sidney-compressed.jpg","short_description": "this is a test","title":"Raiders from the North"}]}
Here is the output
{"product":[{"featured_src":"https://example.com/wp-content/uploads/2015/06/sidney-compressed.jpg","short_description":"this is a test","title":"Raiders from the North"}]}

Parsing a json object with an unknown name with gson

{
"lastUpdated":1404620562,
"invasions":{
"Vibrant Valley":{
"asOf":1404620562,
"type":"Penny Pincher",
"progress":"959/1000"
}
},
"error":null
}
I'm new to using Json & Gson so be patient.
So I am attempting to make an application for myself that allows me to view the information from this json file. The only problem is that it is constantly changing and sometimes there will be more then one object under invasions or sometimes there will be none. How would I parse this with gson? Thanks. Note I am grabbing values from here.
https://www.toontownrewritten.com/api/invasions
Thanks!
Use the JsonParser class
String myJson = "{" +
" \"lastUpdated\":1404620562," +
" \"invasions\":{" +
" \"Vibrant Valley\":{" +
" \"asOf\":1404620562," +
" \"type\":\"Penny Pincher\"," +
" \"progress\":\"959/1000\"" +
" }" +
" }," +
" \"error\":null" +
"}"
JsonElement jelement = new JsonParser().parse(myJson);
JsonObject jobject = jelement.getAsJsonObject();
long lastUpdated = jobject.get("lastUpdated").getAsLong();

Not able to parse the JSON object returned from servlet in AJAX

I am not able to parse tje Json object returned from the servlet in ajax,
I need to put json object values in there relative field
From my java code i am sending the below String in the form of JSON
String webVisitorDetails = "{"+"companyName : "+webVisitor.getCompanyName()+
"address : "+webVisitor.getProfessionalAddress()+
"city : "+webVisitor.getCity()+
"zipCode : "+webVisitor.getZipCode()+
"clientId : "+webVisitor.getCustomerAccountNumber()+ "}";
response.setContentType("application/json");
response.getWriter().write(webVisitorDetails);
In ajax
$.ajax({
url: "ships",
data: {
email: email.toString()
},
success: function(data) {
$.each(data, function(k, v) {
console.log(k + " Value " + v);
$("#city").text(v.city);
$("#zipcode").text(v.getZipCode);
$("#Adress").text(v.getProfessionalAddress);
});
},
error: function(data) {
console.log("error:", data);
},
type: "post"
});
you forgot the comma's and you should quote your values
String webVisitorDetails = "{
\"companyName\": \"" + webVisitor.getCompanyName() + "\",
\"address\": \"" + webVisitor.getProfessionalAddress() + "\",
\"city\": \"" + webVisitor.getCity() + "\",
\"zipCode\": \"" + webVisitor.getZipCode() + "\",
\"clientId\": \"" + webVisitor.getCustomerAccountNumber() + "\"
}";
EDIT: indeed => quote your keys to (just in case)
I'm not a real java expert but yes if there is a class for that use it.
Also... You should not reinvent the wheel. Java has a perfect way of creating "working" JSON.
Map obj=new LinkedHashMap();
obj.put("name","foo");
obj.put("num",new Integer(100));
obj.put("balance",new Double(1000.21));
obj.put("is_vip",new Boolean(true));
obj.put("nickname",null);
String jsonText = JSONValue.toJSONString(obj);
System.out.print(jsonText);
// Result: {"name":"foo","num":100,"balance":1000.21,"is_vip":true,"nickname":null}
Put your keys and values in quotes and seperate your pairs by commas!
Apart from the fact that you JSoN is incorrect (see other answers), you are looping through a response object whereas you should loop through data object that you have used as parameter of success function
In addition to that, in order to loop through data, you must return an array from server i.e. enclose your json object in square brackets ([ ]).
You can see from VDP's answer, your json is not formed correctly. You need to have a comma after each parameter.
A few more pointers which might be helpful:
You can validate if your json is correctly formed or not by pasting it at http://jsonlint.com/
If you are doing a good amount of json processing in your application, I would recommend using a standard JSON library such as json-simple or gson.

Categories