Json Transformation in java with high performance - java

I need to transform a Json into another Json according to the parameter coming as part of Rest request. This service is developed in Java. I know, Jackson API can be used easily and there are some libraries also available. But my requirement is to delivery response with new Json faster as much as possible.
If I can be given few option I can measure the performance of those.
Let's assume I have this Json in data storage:
{
"bookId": "23228232-2dfa232",
"bookName": "Json Transformation",
"bookPublisher": "Tech Publication",
"bookRating": [
{
"source": "All book rank",
"maxRating": "10",
"rating": "3.4"
},
{
"source": "Tech Books",
"maxRating": "5",
"rating": "2"
},
{
"source": "Popular",
"maxRating": "3",
"rating": "1"
}
],
"bookAuthor": [
{
"name": "Jone",
"specialities": [
"Json",
"Javascript",
"Typescript",
"nodejs"
]
},
{
"name": "Mike",
"specialities": [
"Java",
"Spring",
"ElasticSearch"
]
}
]
}
Below rest calls should have respective results from this Json:
Get only authors
/authorName/23228232-2dfa232
{
authorName: [
"Jone",
"Mike"
]
}
Get Average Rating
/popularity/23228232-2dfa232
{
rating: "1.78"
}
So, the question is how to do this kind of transformation efficiently with any available library? As I mentioned above, I can simply use any Json library in Java and transform the Json, but I am not sure, if that will be efficient.

You can try little json java library for searching json data.
JsonValue json = JsonParser.parse(stringvariablewithjsondata);
List<JsonValue> authors = json.findAll(SPM.path("bookAuthor", "name")));
List<String> ratings = json.findAllLiterals(SPM.path("bookRating", "rating")));
and compute result like
JsonArray values = JsonFactory.array();
for(JsonValue value : authors) values.add(value);
JsonObject result = JsonFactory.object().add("authorName", values);
return result.toCompactString()

You can use JSON-Java --> https://www.baeldung.com/java-org-json
Or the Google JSON, aka GSON and a few others as listed here --> https://javarevisited.blogspot.com/2016/09/top-5-json-library-in-java-JEE.html
To see popularity and use statistics, which might help you chose which ones to test first: https://www.baeldung.com/java-json

You can use GSON as it easily maps JSON with POJO classes (Especially nested ones)
For a quick reference for performance comparison,

Related

Deserializing Jackson object where key is value inside larger object

I have a rather large JSON object (this is a subset) I'm trying to parse:
{
"items": [
{
"Name": "Wallet",
"tags": [
"wallet",
"cardholder"
],
"features": {
"material": {
"location": "in-house"
},
"stitching": {
"location": "in-house"
}
},
"color": null,
"store": {
"address": "123 Main Street"
}
}
],
"jItem": 0
}
I have Java POJO's for all JSON objects except for the features object, which contains objects where the key is a dynamic value. My current only-POJO's code does this:
...
itemsJson = doGet(url);
ObjectMapper objMapper = new ObjectMapper();
Items items = objMapper.readValue(itemsJson, Items.class);
...
This gives me a hierarchy of Java POJO's representing my data. The one hitch is features. How can I parse the features data, with the keys as values, within this larger object? I've looked at other SO posts:
Deserialize JSON in Jackson where key is a value
Deserializing jackson dynamic key value
Jackson JSON key as value in Java
but none of these solutions have 1. an object where the key is the value and 2. an object where the key is the value is contained within an object. I do have parsing just the features working using this "just features JSON":
{
"features": {
"material": {
"location": "in-house"
},
"stitching": {
"location": "in-house"
}
}
}
with this code:
...
JsonNode jsonNodeRecord = objectMapper.readTree(App.class.getResourceAsStream("/data.json"));
List<JsonNode> recordNodes = jsonNodeRecord.findValues("features");
...
which gives me JsonNode's. This isn't ideal because I don't have my features data in a POJO.
The Question:
It's not clear to me how to integrate parsing the JSON using POJO's for everything except for features, with either the JsonNode code above or a custom deserializer as in the #1 SO link above.

Parsing JSON object from service call Spring JAVA

I am getting the following response from some service call . I am trying to parse the JSON . I am actually new to JAVA and not sure about how to parse JSON objects returned from HTTP call . I am getting the following error :
org.json.JSONException: JSONArray initial value should be a string or collection or array.
at org.json.JSONArray.<init>(JSONArray.java:197) ~[json-20180813.jar!/:na]
Code :
Object resp = hiveApiClient.getEnrollmentSearchDetails(certificate, employeeId);
logger.info("response : " + resp);
JSONArray mainArray = new JSONArray(resp);
// The nested array is at the second position : 1
JSONArray nestedArray = mainArray.getJSONArray(1);
// the interesting main JSONObject is on the first position
// of the nested array : 0
JSONObject interestingJSONObject = nestedArray.getJSONObject(0);
logger.info("XXX :{}", interestingJSONObject);
String courseId = interestingJSONObject.getJSONObject("additionalData").getString("courseId");
logger.info("XXXX :{}",courseId);
return courseId;
Response :
[
"list", [{
"#type": "com.saba.services.calendar.CalendarElementDetail",
"eventType": "ILTCLASS",
"elementName": "Microservice Application Architecture",
"elementId": "class000000000013497",
"eventId": "timel000000000103609",
"ownerID": "emplo000000000096641",
"locationId": "locat000000000003165",
"locationName": "IND-Bangalore-Karnataka",
"additionalData": {
"#type": "map",
"locationTimeZone": "tzone000000000000042",
"eventID": "class000000000013497",
"locationName": "IND-Bangalore-Karnataka",
"locationId": "locat000000000003165",
"transcriptID": "ofapr000000002962367",
"registrationID": "regdw000000001766254",
"eventName": "Microservice Application Architecture",
"moduleID": "regmd000000002147176",
"courseID": "cours000000000031995"
},
"startDate": {
"#type": "com.saba.customtypes.DateWithLocale",
"date": 1538613000000,
"locale": "03-OCT-2018",
"timeInLocale": "8:30 PM",
"dateInUserTimeZone": "03-OCT-2018",
"timeInUserTimeZone": "5:30 PM",
"dateInCustomTimeZone": null,
"timeInCustomTimeZone": null,
"customTimeZoneDate": 0,
"timeInStandardFormat": "8:30 PM",
"dateInStandardFormat": "10/03/2018"
}
}]
]
Well first of all, your json is not valid because of this}:
["list" : /* something here but anyway, not the concern here */ ]
when it should have been
{"list" : /* something here but anyway not the concern here */}
I think your problem is with the understanding of how a JSON file works and what is a json object and a json array. Please correct your JSON input so that we can provide you with insights on how to retrieve the value you need.
Additionally, I would recommend you looking into Jackson lib for parsing JSON objects to JAVA POJOs directly really easily. The link is a great tutorial to get you started here. Furthermore, jackson is already included with Spring so that you literally have nothing to install.
Edit
I misread the JSON input : I saw a : after "list" instead of a ,.
So your JSON is a proper JSON but its a quite uncommon JSON as it is loosely typed and therefore cannot be that easily parsed with standard Jackson library for example. In fact, in the main array, a string is put together with a Json Object which is a very bad practice but that's not your fault as I suppose you are not responsible for the output of this HTTP call.
So how can you actually get your value ? Well let's describe the JSON, you've got here : a JSON array containing a String and another sub JSON array. You want to take some values from the very first JSON object inside the nested json array.
This one :
{
"#type": "com.saba.services.calendar.CalendarElementDetail",
"eventType": "ILTCLASS",
"elementName": "Microservice Application Architecture",
"elementId": "class000000000013497",
"eventId": "timel000000000103609",
"ownerID": "emplo000000000096641",
"locationId": "locat000000000003165",
"locationName": "IND-Bangalore-Karnataka",
"additionalData": {
"#type": "map",
"locationTimeZone": "tzone000000000000042",
"eventID": "class000000000013497",
"locationName": "IND-Bangalore-Karnataka",
"locationId": "locat000000000003165",
"transcriptID": "ofapr000000002962367",
"registrationID": "regdw000000001766254",
"eventName": "Microservice Application Architecture",
"moduleID": "regmd000000002147176",
"courseID": "cours000000000031995"
},
"startDate": {
"#type": "com.saba.customtypes.DateWithLocale",
"date": 1538613000000,
"locale": "03-OCT-2018",
"timeInLocale": "8:30 PM",
"dateInUserTimeZone": "03-OCT-2018",
"timeInUserTimeZone": "5:30 PM",
"dateInCustomTimeZone": null,
"timeInCustomTimeZone": null,
"customTimeZoneDate": 0,
"timeInStandardFormat": "8:30 PM",
"dateInStandardFormat": "10/03/2018"
}
}
The first task here is to gather this object. Let's suppose the nested json array is always in the second position after the string and that the JSON object you want is always at the first position of the nested array which might not be the case depending on your input JSON but this was not precised in your question.
JSONArray mainArray = new JSONArray(resp);
// The nested array is at the second position : 1
JSONArray nestedArray = mainArray.getJSONArray(1);
// the interesting main JSONObject is on the first position
// of the nested array : 0
JSONObject interestingJSONObject = nestedArray.getJSONObject(0);
So now we want "courseId" from "additionnalData" Json Object :
String courseId = interestingJSONObject.getJSONObject("additionalData").getString("courseId");
And there you go!

JSONLD: How to convert a json into JsonLD?

I have a use case where I have a Json data and I have to convert that into JSONLD format.
First Question : Can this be done easily , like some API for this, that probably I am missing ?
Second Question : If not then what are the steps that needs to be taken.
So the Json Looks like :
{
key:"language",
value: "scala"
}
And I want to convert it into the JSONLD format.
Any help is appreciated.
You can simply add a context on this json object like :
{
#context: {
"key": "http://schema.org/description",
"value": "http://schema.org/value"
},
key: "language",
value: "scala"
}
If you are interested in using JavaScript library to do this task, then npm module JSONLD is great one.
Note: This library has dependency on PYTHON.
JavaScript code Example from JSONLD site:
var doc = {
"http://schema.org/name": "Manu Sporny",
"http://schema.org/url": {"#id": "http://manu.sporny.org/"},
"http://schema.org/image": {"#id": "http://manu.sporny.org/images/manu.png"}
};
var context = {
"name": "http://schema.org/name",
"homepage": {"#id": "http://schema.org/url", "#type": "#id"},
"image": {"#id": "http://schema.org/image", "#type": "#id"}
};
// compact a document according to a particular context
// see: http://json-ld.org/spec/latest/json-ld/#compacted-document-form
jsonld.compact(doc, context, function(err, compacted) {
console.log(JSON.stringify(compacted, null, 2));
});
Output:
{
"#context": {...},
"name": "Manu Sporny",
"homepage": "http://manu.sporny.org/",
"image": "http://manu.sporny.org/images/manu.png"
}
When I was searching for JavaScript library for the JSON to JSONLD transformation, I could come up with this one. But, as this library has PYTHON dependency, I am searching for other JavaScript library for the same.

How to escape HTML in JSON

I'm wondering how can I escape HTML code in JSON ? I'm using Jackson as my JSON mapper.
In my content I have various characters: tags, single quotes, double quotes, new lines character (\n), tabs etc. I tried to use CharacterEscapes class, but with no results.
My JSON response blows up after using CharacterEscapes. I tried to escape it manually, but also without any results.
So the question is, lets say that we have:
<p>Some text</p>\n<p>"SomeText"</p>
How can I send it back to browser as value of the JSON object?
UPDATE:
Input is:
{
"code": {
"num": 12
},
"obj": {
"label": "somelabel",
"order": 1
},
"det": {
"part": "1",
"cont": true
},
"html": "<p>Mine text</p>"
}
Output:
{
"code": {
"num": 12
},
"obj": {
"label": "somelabel",
"order": 1
},
"det": {
"part":"1",
"cont": true
},
"html":{"code": {
"num": 12
},
"obj": {
"label": "somelabel",
"order": 1
},
"det": {
"part":"
}
For now I have found following solution:
I have added CharacterEscapes to the JsonFactory of the ObjectMapper class.
Also I have changed way of writting JSON into response.
Instead of
objectMapper.writeValue(response.getWriter(), myObject)
I'm doing this:
PrintWriter writer = response.getWriter();
writer.print(String.valueOf(objectMapper.writeValueAsBytes(myObject));
writer.flush();
And it works as I wanted.
I would suggest to use either of below.
1.You can use GSON library for this purpose.
Gson gson = new GsonBuilder().disableHtmlEscaping().create();
2.Use Apache commons StringEscapeUtils.escapeHtml("JSON string").

gson json parser: value without key

I want to parse a json structure like this:
{"response":
{
"arr1":[count,{...}],
"arr2":[count,{...}]
}
}
Everything is ok if count have key "count" (for example). But the key is blank. Is it any way to map this structure and manually retrive only this value or i need to parse all of this json myself without gson ?
UPDATED
Here is a valid json (checked with http://jsonlint.com/)
{
"response": {
"arr1": [
615,
{
"body": "hi",
"title": "Re(2): ..."
},
{
"body": "hello",
"title": "Re(23): ..."
}
],
"arr2": [
132,
{
"body": "hi",
"title": "Re(2): ..."
},
{
"body": "hello",
"title": "Re(23): ..."
}
]
}
}
If you want to parse arbitrary collections you should read Serializing and Deserializing Collection with Objects of Arbitrary Types and look at the example code example code.

Categories

Resources