I want to compare Request object and DB object in jersey PUT/PATCH request, so that requested object i have converted in Map{String,Object} reqObject, and DB object also i converted in Map{String,Object} db object.
Now I want to compare both map and print the diff value in log.
Please help me how to do in generic way.
if(!properties1.equals(properties2)){
for(Map.Entry<String, Object> empNew:properties1.entrySet()){
for(Map.Entry<String, Object> empOld:properties2.entrySet()){
if((empNew.getKey().equals(empOld.getKey())&& !(empNew.getValue().equals(empOld.getValue())))){
System.out.println("Update "+" "+empNew.getKey()+" "+"value"+" "+"from"+" "+empOld.getValue()+" "+"to"+" "+empNew.getValue());
}
}
}
}
My object Structure is like this:
{"id" : "5349b4ddd2781d08c09890f3",
"name" : "Abed",
"address" : [{ "number" : "xyz",
"street" : "asdfgh",
"town" : "AMB6 Ind2",
"postcode" : "600039" }
]
}
Related
I need to get the common elements from List<Map<String,Object>> and print that result. Below is the example where the result would be some json.
Ex:
[
{
"Id" : "123",
"common" : [
{
"group":"abc",
"department" : "xyz"
},
{
"group":"abc",
"department" : "zyx"
}
]
]
Depends what the type of Object
List<Map<String,Object>> o = ...
Object common = o.get(0).get("common");
// if object is of type Map<String, String>
Map<String,Object> data = (Map<String,Object>) common;
I've got this json object
{
"type" : "employee",
"columns" :
[
{
"id" : 1,
"human" :
[
{
"name" : "ANA",
"age" : "23"
},
{
"name" : "IULIA",
"age" : "22"
}
]
},
{
"id" : 2,
"human" :
[
{
"name" : "ADI",
"age" : "21"
},
{
"name" : "GELU",
"age" : "18"
}
]
}
]
}
and I need to extract the first name from each human list.
I've tried .body("columns.human.name[0]", everyItem(containsString("A"))) but it doesn't work. Any ideas?
Using JsonPath you can get all columns and all humans.
Each of JSON Object is represented as HashMap<>. If it contains only fields it's HashMap<String, String> but if contains arrays or nested JSON Objects then it is HashMap<String, Object> where Object is either another JSON Object or array.
Given the above you can use following code to get all columns and name of first human in each column:
JsonPath path = response.jsonPath();
List<HashMap<String, Object>> columns = path.getList("columns");
for (HashMap<String, Object> singleColumn : columns) {
List<HashMap<String, Object>> humans = (List<HashMap<String, Object>>) singleColumn.get("human");
System.out.println(humans.get(0).get("name"));
}
The above code will print ANA and ADI in the console.
You can store the results in List<String> for further processing
you can get all "name" from humans with jsonPath : $.columns[*].human[*].name it will give below result :
[
"ANA",
"IULIA",
"ADI",
"GELU"
]
And if you want only first "name" then you need to use josn : $.columns[*].human[0].name this will give you below result:
[
"ANA",
"ADI"
]
I have the following JSON data for one of sample mongo record:
{
"_id" : ObjectId("598b53a1e4b0d3153fe5375a"),
"_class" : "com.Request",
"administratorComments" : "",
"statusLog" : [
{
"status" : "Submitted",
"startDate" : ISODate("2017-08-09T18:25:37.870Z"),
"endDate" : ISODate("2017-08-10T15:40:08.495Z")
},
{
"status" : "Backlogged",
"startDate" : ISODate("2017-08-10T15:40:08.495Z"),
"endDate" : ISODate("2017-09-15T18:18:14.241Z")
},
{
"status" : "Deleted",
"startDate" : ISODate("2017-09-15T18:18:14.241Z"),
"endDate" : ISODate("2017-09-15T18:18:24.764Z")
}
],
"lastChangeDate" : ISODate("2017-09-15T18:29:35.886Z"),
"createdDate" : ISODate("2017-08-09T18:25:37.870Z")
}
I am fetching the different status inside statusLog for different _id using a Hashmap:
HashMap<String, List<String>> hm_mongo = new HashMap<String, List<String>>();
Where I am storing _id as Key and different status in an Array<List>.
Now, I am supposed to capture the endDate for all the status.
Could you point any data structure that will be helpful to capture the status and its endDate for different _ids for MongoDB?
You can simply expand your current HashMap's value structure. In order to do this, you can create a class that will store the "status", "startDate", "endDate" fields.
class statusLog{
public string status;
public Date startDate;
public Date endDate;
}
HashMap<String, List<statusLog>> hm_mongo = new HashMap<String, List<statusLog>>();
Lets say that you have a json array that looks as follows :
[
{
"id" : 1,
"first_name" : "Jane",
"last_name" : "Doe"
},
{
"id" : 2,
"first_name" : "John",
"middle_name" : "Q",
"last_name" : "Public",
"birth_year" : 1971
},
{
"id" : 3,
"anonymous_user" : true,
"crm_id" : "abc123"
},
{
"id" : 4,
"first_name" : "Albert",
"last_name" : "Einstein",
"profession" : "Scientist",
"birth_year" : 1879,
"e_equals_mc_squared" : true
}
]
The goal is to use Jackson to marshal to POJO. My thinking is that I could have a class to contain each K,V pair .. something like :
public class myDataObject {
private String key;
private T value;
...
}
And maybe a container class for that :
public class myDataContainer {
private ArrayList<myDataObject> dataList;
...
}
My question becomes what does marshaling that look like using jackson? There is no schema for the json, each json object can have an unspecified number of K,V pairs and the list of keys is also unspecified.
Does something like this work? Is this even the right approach?
ArrayList<myDataContainer> dataList = mapper.readValue(jsonFile, new TypeReference<ArrayList<myDataContainer>() {});
What your JSON really is, is a list of maps - with Strings as keys and Objects as values.
So, using Jackson, you should be able to do:
ObjectMapper mapper = new ObjectMapper();
List<Map<String, Object>> data = mapper.readValue(json, new TypeReference<List<Map<String, Object>>>(){});
I am working in Cloudera Manager Navigator REST API where extracting result is working fine, but unable to get any nested value.
The type of data is extracting as below.
{
"parentPath": "String",
"customProperties": "Map[string,string]",
"sourceType": "String",
"entityType": "String"
}
And data should be like
{
"parentPath": "abcd",
"customProperties": {
"nameservice" : "xyz"
},
"sourceType": "rcs",
"entityType": "ufo"
}
But I am getting key-value result as follows.
parentPath :abcd
customProperties : null
sourceType : rcs
entityType : ufo
In above response data, "customProperties" is coming with a null value where it should return a map object contains ["nameservice" : "xyz"]. This is the problem with following code snippet.
MetadataResultSet metadataResultSet = extractor.extractMetadata(null, null,"sourceType:HDFS", "identity:*");
Iterator<Map<String, Object>> entitiesIt = metadataResultSet.getEntities().iterator();
while(entitiesIt.hasNext()){
Map<String, Object> result = entitiesIt.next();
for(String data : result.keySet()){
System.out.println(" key:"+data+" value:"+result.get(data));
}
}
Can you suggest me how to get the nested value where datatype is complex.
have u checked how the data looks on navigator ui? You can first verify that once, and also try cloudera /entities/entity-id rest API in browser to check how json response is coming