Flattening of json to a map with Jackson - java

I am currently using the Jackson library to unmarshal json to pojos, using annotations.
The json tree that I would like to unmarshal is as follows:
{
"key1":"value1",
"key2":"value2",
"key3":{
"key31":{
"key311":"value311",
"key312":"value312",
"key313":"value313"
},
"key32":"value32"
},
"key4":"value4",
"key5":"value5",
"key6":{
"key61":"value61"
}
}
I don't know the json structure in advance and would like to completely flatten it to a Map which content would be equivalent to:
Map<String, Object> outputMap = new HashMap<String, Object>();
outputMap.put("key1", "value1");
outputMap.put("key2", "value2");
outputMap.put("key311", "value311");
outputMap.put("key312", "value312");
outputMap.put("key313", "value313");
outputMap.put("key32", "value32");
outputMap.put("key4", "value4");
outputMap.put("key5", "value5");
outputMap.put("key61", "value61");
(note that keys "key3", "key31" and "key6" should be ignored)
Using the annotation #JsonAnySetter, I can create a function to populate the map with all top level atoms, but the method will also catch the node having children (when the value is a Map).
From that point, I can of course write myself the simple recursion over the children, but I would like this part to be handled automatically (in an elegant way) through the use of a facility from the library (annotation, configuration, etc.), and not have to write the recursion by myself.
Note: we assume there is not name clashing in the different level keys.

There is no annotation-based mechanism to do this that I know of, since #JsonUnwrapped which might be applicable is only usable with POJOs, not Maps.
I guess you could file an enhancement request to ask #JsonUnwrapped to be extended to also handle Map case, although it seems only appicable for serialization (not sure how one could deserialize back).

Related

Variable Names In JSON List Followed By Object Details

I've got a curious JSON to work with that I need to be able to map to a Java object. The environment I'm working in doesn't have access to Guava's Multimap (if that even is a solution), and I've considered being able to extend some sort of base class with a variable class name (if that is even possible), but I'm out of my depth on this one.
What sort of Java object allows lists of objects with unique, varying references to the same object class?
Here's a sample of the JSON I'm working with, I've confirmed it's a valid JSON via JSON formatter:
{
"apple1":{
"orchard":"green groves orchard",
"zipcode": 34567,
"speciesId": 12345,
"applePickedNumber": 6437896,
"knownProducts":{
"green grove apple butter":{
"productId":"ABC123456789",
"manufacturer":"red barn cannery",
"shipper":"hermes shipping"
}
}
},
"apple2":{
"orchard":"fair pastures orchard",
"zipcode": 34567,
"internalSpeciesId": 10001,
"speciesId": 23456,
"applePickedNumber": 145,
"knownProducts":{}
}
}
Thanks in advance for your help!
I think that the answer is probably Object[] and Map<String,Object>. But if you are going to map your JSON to types like that, then you should probably abandon the idea of mapping, and just use the JSONObject and JSONArray types.
Mappings only really work if the JSON conforms to a fixed "schema" with fixed attribute names. You can't map JSON to POJO classes if the attribute names keep changing.
Example when using Jackson. Define other attributes in Object class
ObjectMapper objectMapper = new ObjectMapper();
TypeReference<HashMap<String, Object>> typeRef
= new TypeReference<>() {
};
HashMap<String, Object> result = objectMapper.readValue(json, typeRef);

Java POJO attributes mapping

I have a use case where I receive some attributes in the request like this,
"filters": [
{
"field": "fName",
"value": "Tom"
},
{
"field": "LName",
"value": "Hanks"
}
]
I don't have a model defined for this. I just receive these attributes in the request and fire a query on elastic search using these attributes. My records in elastic search have the same attribute names.
Now, I have to support a legacy application where attribute's names are completely different.
E.g.: fName becomes firstName and lName becomes lastName.
Problem: Need to accept old attribute names in the request, convert them to new ones so that it matches my elastic search records. Fetch the data with new attribute names and convert back to old ones before sending out the response from the application.
NOTE: I don't have POJO's defined for these records.
How can this be achieved effectively? I was thinking of using Orika mapper but not sure how that will work without defining classes first.
What prevents you from writing a transformer from request JSON to your normalized JSON?
The normal flow I can think of is:
Request JSON -> POJO -> POJO with normalized value -> Normalized JSON
So your POJO looks like:
public class Filter {
List<FieldFilter> filters;
public static class FieldFilter {
private String field;
private String value;
}
}
Now you will have a transformation map like:
Map<String, String> fieldNameMapping = new HashMap<>();
fieldNameMapping.put("fName", "firstName");
fieldNameMapping.put("firstName", "firstName");
// The process of populating this map can be done either by a static initializer, or config/properties reader
Then you transform your POJO:
Filter filterRequest;
List<FieldFilters> normlizedFilters =
filterReq.getFilters().stream()
.map(f -> new FieldFilter(fieldNameMapping.get(f.getField()), f.getValue())
.collect(toList());
Then convert the Filter class to your normalized JSON.
We have a similar scenario and we are using apache JOLT.If you want to try some samples, you can refer jolt-demo-online-utility
Use a JSON to JSON-transformer instead. Good answers regarding this can be found here: JSON to JSON transformer and here : XSLT equivalent for JSON
In the end you do not require an intermediate object type here. You even said, that you do not have such a type yet and inventing it, just to transform it, doesn't really make sense.

Best practice generating object for response

I have rest server with spring.
There is a lot of requests where one of the params is fields fields is the set of fields that server should return in response. like: /?fields=[id,name] and server should return JSON object with both fields
I would like to know what is the best practice for generating such response.
We do it like this:
private Map<String, Object> processBook(BookEntity book, Set<String> fields, String locale){
Map<String, Object> map = new HashMap<String, Object>();
//..
if(fields.contains(ID)){
map.put(ID, book.getId());
}
if(fields.contains(ISBN)){
map.put(ISBN, book.getIsbn());
}
if(fields.contains(DESCRIPTION)){
if(locale.equals(UserLocale.UK)) map.put(DESCRIPTION, book.getDescriptionUa());
else if(locale.equals(UserLocale.RU)) map.put(DESCRIPTION, book.getDescriptionRu());
else map.put(DESCRIPTION, book.getDescriptionEn());
}
//..
return map;
}
Maybe there is much better alternative?
Note that in your case you obtain all data from DB - fully filled BookEntity object, and then show only requested fields.
In my opinion it'd be "much better alternative" to delegate field list to appropriate downstream integration call and get BookEntity object only with necessary fields. Then mentioned above method will reduce to just one line, your DB responses will be more lightweight, so it will bring simplicity and optimization gain to your system.
Any adequate DB provides such functionality: SQL or NoSQL, etc.
P.S. Plus standard approach of Object to JSON mapping such as Jackson or GSON at top level.
Instead of having a Map, you could have and object with the attributes you need and set them, instead of adding to map.Then you can use Google's Gson to transform your object into a Json object.Take a look at this quick tutorial.
One approach is to have an asMap function.
Map<String, Object> map = book.asMap();
map.keySet().retainAll(fields);

Filter single element from a multi value element in JSON

In my test I need to compare the expected and actual JSON response. But the JSON response is limited depending upon the role. So I need to exclude certain fields while comparing.
Below is the JSON and I want to filter out 2 things from it.
1. CompanyId
2. status.
{
userId=dg4d6g4dg45-rgdre-543-dfg,
userName=test123,
effectives=[
{
companyId=345634-54-547-74,
companyName=xyz,
roleId=685-345863490-634,
roleName=This is the test Role
},
{
companyId=345634-54-547-74,
companyName=xyz,
roleId=685-345863490-634,
roleName=This is the test Role
}
],
status=Active
}
Can you someone please let me know how to achieve this.
I explored the filterOutAllExcep method of SimpleBeanPropertyFilter but then I will have to figure out the logic to remember all the fields that should be included as well.
I think i have found a resolution for this.
I am using Object Mapper and converting the data model into a Map.
Then iterating over the map and removing the fields.
Flat filtering is straight forward but had to right up some logic for nested fields.
// Convert the DataModel into a Map object
ObjectMapper mapper = new ObjectMapper();
Map mainObjectMap = mapper.convertValue(object, Map.class);

Constructing POJO out of JSON string with dynamic fields using Gson

I'm consuming a web service in my application that will return a list of ID's associated with a name. An example would look like this:
{
"6502": "News",
"6503": "Sports",
"6505": "Opinion",
"6501": "Arts",
"6506": "The Statement"
}
How would I construct a POJO for Gson to deserialize to when all of the fields are dynamic?
How about deserializing into a map?
Gson gson = new Gson();
Type mapType = new TypeToken<Map<String, String>>() {}.getType();
String json = "{'6502':'News','6503':'Sports','6505':'Opinion','6501':'Arts','6506':'The Statement'}";
Map<String, String> map = gson.fromJson(json, mapType);
Using a map sounds reasonable for me (as Java is statically typed). Even if this could work (maybe using JavaCompiler) - accessing the object would not probably be much different from accessing a map.
I don't know Gson that well but I suspect that's not possible. You'd have to know which fields are possible beforehand, although the fields might not be in the Json and thus be null.
You might be able to create classes at runtime by parsing the Json string, but I don't know whether that would be worth the hassle.
If everything is dynamic your best bet would be to deserialize the Json string to maps of strings or arrays etc. like other Json libraries do (I don't know whether Gson can do this as well, but the classes you need are commonly called JSONObject and JSONArray).
So your Json string above would then result in a Map<String, String>.

Categories