I have a json response that comes under this form.
"calendarList": [
{
"id": "1",
"event": {
"id": "11",
"name": "Track the working hours",
"color": "blue",
"place": "office",
}
},
{
"id": "2",
"event": {
"id": "12",
"name": "Finish DTOs",
"color": "blue",
"place": "office",
}
}
]
How can I access the place value? For all of dto objects inside the list the place value will be the same, which is OFFICE, so I just need to access somehow one of the place values.
The list of CalendarDto contains multiple EventDto objects, with the attributes id, name, color and place.
final List<CalendarDto> calendarList = calendarService.getCalendars(startDate, endDate);
EventDto eventDto = new EventDto();
EventSender sender = new EventSender ("EVENTS");
sender.withData("place", calendarList.stream() -> this is where I tried to get the value, but it didn't work
.findFirst(eventDto.getPlace()));
You can use combination of filter and findFirst methods of stream.
final List<CalendarDto> calendarList = calendarService.getCalendars(startDate, endDate);
EventDto eventDto = new EventDto();
EventSender sender = new EventSender ("EVENTS");
sender.withData("place", calendarList.stream()
.filter(ele -> ele.getPlace().equals(eventDto.getPlace())).findFirst());
Note:- ele into filter method will be object of CalendarDto class, here respective method is required to be replaced to get place value.
Related
I have a Json body like the example below. I need to extract the value from a key that has another key with a specific value in an array. I am passing in a JsonNode with everything in the detail component of the message, I can easily extract from each level, however, I'm struggling with the array.
In this case, I need to extract the value of "value" (Police/Fire/Accident Report) from the object in the array which has a key/value pair of "name":"documentTitle". I understand this is a JSONArray, but I can't find a good example that shows me how to extract the values for an object in the array that contains a certain key/value pair, I don't think I can rely on getting the object in position [2] in the array as the same objects may not always be present in the additionalMetadata array.
Sample Json:
"sourceVersion": "1.0",
"eventId": "8d74b892-810a-47c3-882b-6e641fd509eb",
"clientRequestId": "b84f3a7b-03cc-4848-a1e8-3519106c6fcb",
"detail": {
"stack": "corona",
"visibilityIndicator": null,
"documentUid": "b84f3a7b-03cc-4848-a1e8-3519106c6fcb",
"additionalMetadata": [
{
"name": "lastModifiedDate",
"value": "2021-05-21T04:53:53Z"
},
{
"name": "documentName",
"value": "Police/Fire Report, 23850413, 2021-05-20 14:51:23"
},
{
"name": "documentTitle",
"value": "Police/Fire/Accident Report"
},
{
"name": "documentAuthor",
"value": "System Generated"
},
{
"name": "lastModifiedBy",
"value": "System Updated"
},
{
"name": "createdBy",
"value": "System Generated"
},
{
"name": "documentDescription",
"value": "Police/Fire Report received"
},
{
"name": "organizationCode",
"value": "Claims"
}
]
}
}```
Loop through the json array and extract the json object with name documentTitile. From that json object you can get the value
Well, either the JSON framework you're using supports this out of the box (check the documentation) or you could convert it manually to a map:
List<AdditionalMetadataEntry> additionalMetadata;
[...]
Map<String, String> additionalMetadataMap = additionalMetadata.stream().collect(Collectors.toMap(AdditionalMetadataEntry::getName, AdditionalMetadataEntry::getValue));
I was able to figure it out. I created a new node off the existing notificationBody JsonNode, then parsed through the metadata key/value pairs:
String docTitle = "";
JsonNode additionalMetadata = notificationBody.get("detail").get("additionalMetadata");
for (JsonNode node: additionalMetadata) {
String name = node.get("name").asText();
String value = node.get("value").asText();
if(name.equals("documentTitle")){
docTitle = value;
}
}
I have a table called Group and it will have records like:
{
"id": "UniqueID1",
"name": "Ranjeeth",
"emailIdMappings": [
{
"emailId": "r.pt#r.com",
"userId": 324
},
{
"emailId": "r1.pt#r.com",
"userId": 325
}
]
},
{
"id": "UniqueID2",
"name": "Ranjeeth",
"emailIdMappings": [
{
"emailId": "r1.pt#r.com",
"userId": 325
},
{
"emailId": "r2.pt#r.com",
"userId": 326
}
]
}
I need to query and get result if emailId contains the input string.
I have reached so far and I am not able to get the result
AttributeValue attributeValue = new AttributeValue("r.pt#r.com");
Condition containsCondition = new Condition()
.withComparisonOperator(ComparisonOperator.CONTAINS)
.withAttributeValueList(attributeValue);
Map<String, Condition> conditions = newHashMap();
conditions.put("emailIdMappings.emailId", containsCondition);
ScanRequest scanRequest = new ScanRequest()
.withTableName("Group")
.withScanFilter(conditions);
amazonDynamoDB.scan(scanRequest)
dynamoDBMapper.marshallIntoObjects(Group.class, scanResult.getItems());
For the above code I am expecting record with id UniqueID1, but it's empty. If you pass "r1.pt#r.com" then you should get both records.
sdk used is com.amazonaws:aws-java-sdk-dynamodb:1.11.155
I tried posting the question in aws forum which didn't help much.
As you have List of Objects which has two attributes in a object (i.e. emailId and userId), you need to provide both values in order to match the item.
The CONTAINS function will not be able to match the item if the object has two attributes and only one attribute value mentioned in the filter expression.
Otherwise, you need to provide the occurrence (i.e. index) of the list to match the item.
Example:-
emailIdMappings[0].emailId = :emailIdVal
I am building an Android application which reads from themoviedb.org.
What I am trying to do is have the user enter a movie title and use that title to find its id.
When I run the query to search for movies, I get a response like:
{
"page": 1,
"results": [
{
"poster_path": "aaaaa.jpg",
"id": "11",
"description": "MovieDescription"
},
{
"poster_path": "bbbbb.jpg",
"id": "12",
"description": "MovieDescription2"
},
{
"poster_path": "ccccc.jpg",
"id": "13",
"description": "MovieDescription"
}
]
}
Using the Maven JSON library, I can fetch the results key as a string using json.get("results").
returning:
[
{
"poster_path": "aaaaa.jpg",
"id": "11",
"description": "MovieDescription"
},
{
"poster_path": "bbbbb.jpg",
"id": "12",
"description": "MovieDescription2"
},
{
"poster_path": "ccccc.jpg",
"id": "13",
"description": "MovieDescription"
}
]
But I want to convert the first of these results to another JSONObject so that I can get the movie's id from the first result.
I'm thinking that the way to do this is to convert the results value to a list of JSONObject and then use the json.get("id") method on the first object in the list. But I do not know how to do this conversion.
Any help would be appreciated.
You can use JSONObject.getJSONArray to get the result directly as a JSON Array:
JSONArray results = json.getJSONArray("results") // Get results as JSON Array
JSONObject first = results.getJSONObject(0) // Get first object as JSON Object
See: JSONObject#getJSONArray(String)
"transaction": {
"id": 1,
"empid": "12345",
"details1": {
"name": "xyz",
"age": "30",
"sex": "M",
"Address": {
"Office": "office",
"Home": "Home"
}
},
"abcDetails": "asdf",
"mobile": 123455
},
I need to test if JSON record contains more then two keys(details, Address).
Then, I need to pass those key input to this line:
parserValue1 = parserValue.asObject().get("firstKey").asObject().get("secondKey");
Can anyone help me?
Many json parsers have a has("key") or contains("key") accessor.
Otherwise you will have to add a condition to check if get("") returns null, or turn your whole Json object into a map, where you do the same checks.
I try to load contacts from a given account using the REST api of sugarcrm using Java. This "kind of" works... My test scenario should return two records. It does, but the records do a) not contain all fields and b) all the returned fields have empty values:
See the entry_list part of the returned JSON:
{"entry_list": [
{
"id": null,
"module_name": "Contacts",
"name_value_list": {
"name": {
"name": "name",
"value": ""
},
"deleted": {
"name": "deleted",
"value": 0
},
"do_not_call": {
"name": "do_not_call",
"value": "0"
}
}
},
{
"id": null,
"module_name": "Contacts",
"name_value_list": {
"name": {
"name": "name",
"value": ""
},
"deleted": {
"name": "deleted",
"value": 0
},
"do_not_call": {
"name": "do_not_call",
"value": "0"
}
}
}
Here is what I set as rest_data in my request:
rest_data.put("session", sessionId);
rest_data.put("module_name", moduleName);
rest_data.put("module_id", sourceId);
rest_data.put("link_field_name", relationField);
rest_data.put("related_module_query", "");
rest_data.put("related_fields", Arrays.asList());
rest_data.put("related_module_link_name_to_fields_array", Arrays.asList());
rest_data.put("offset", 0);
rest_data.put("order_by", "name ASC");
rest_data.put("limit", 0);
So I'd expect that I receive:
- all records -> works
- all fields for every record -> works not
- all values for all fields for every record -> works not
I'm using v4_1 of the REST api.
Does anybody have some hints on this?
Unlike get_entry_list works you cannot specify an empty array for the related_fields parameter. You must specify at least one field name.