I am using JAVA 7.
From HashMap>> data = new HashMap<>(); i am getting below Output
Here map contains dynamic records for days.
Here in array, first value is category1, second value is category2, third value is category3, fourth value is category4 .
{
11/20/17={
producer1=[
]
},
01/01/18={
producer1=[
1, //category1
1, //category2
1, //category3
1 //category4
],
producer2=[
5,
1,
9,
1
]
},
01/08/18={
producer2=[
1,
6,
1,
3
],
}
}
I want to produce output like below for all categories
{producerType : producer1
category1Data : [ 0,1,0]}, // 11/20/17 = 0,01/01/18 = 1,01/08/18 = 0 for category 1.
{producerType : producer2
category1Data : [ 0,5,1]},
Using Jackson API
ObjectMapper mapperObj = new ObjectMapper();
convert map to JSON String like
String jsonResp = mapperObj.writeValueAsString(hashMap);
Related
I want to make a string from json into an object of my class. The problem is, in the class I use an ArrayList and that's why (I think) I get the error message "Can't deserialize JSON array into class". How exactly can I separate the array and convert it into an ArrayList?
#POST
public Response createMocktail(String m){
MocktailDto mocktail = jsonb.fromJson(m, MocktailDto.class);
return Response.ok(mocktailManager.createMocktail(mocktail)).build();
}
Json String:
[
{
"id": 3,
"name": "Mojito",
"zutaten": [
{
"anzahl": 1,
"id": 5,
"name": "Rum"
},
{
"anzahl": 1,
"id": 6,
"name": "GingerAle"
}
]
}
]
JSONObject jsonObj = new JSONObject(m); does not work, it says constructor is undefined although I saw a few solutions like this
The problem is your input string is Array (when it starts with [)
There are a few possible solutions:
First:
MocktailDto[] data = jsonb.fromJson(m, MocktailDto[].class);
data[0];
Second:
Type listType = new TypeToken<ArrayList<MocktailDto>>(){}.getType();
ArrayList<MocktailDto> data = jsonb.fromJson(m, listType);
data.get(0);
I have a document already in the database that can be represented as follows:
{
_id: 1,
field1: "hello"
field2: 2,
listField: [
{
subField1: ...,
subField2: ...
},
{
subField1: ...,
subField2: ...
}
]
}
I have constructed another document that will be used to update the document with an id of 1. It can be represented as follows:
Document newDocument = {
field1: "goodbye",
field3: 3,
listField: [
{
subfield3: ...
},
{
subfield3: ...
}
]
}
The expected result is as follows:
{
_id: 1,
field1: "goodbye"
field2: 2,
field3: 3,
listField: [
{
subField1: ...,
subField2: ...,
subField3: ...
},
{
subField1: ...,
subField2: ...,
subField3: ...
}
]
}
How can this be accomplished using the Java driver? I had previously attempted to accomplish this with
collection.updateOne(Filters.eq("_id", new ObjectId(1)), new Document("$set", newDocument));
However, this method produces the following result instead:
{
_id: 1,
field1: "goodbye"
field2: 2,
field3: 3,
listField: [
{
subField3: ...
},
{
subField3: ...
}
]
}
The issue with this is that the listField field is being replaced with the new field. I want each document within listField to be updated with the new sub fields instead. What should I be doing differently to accomplish this?
The table I am having is of the following form
{
"element_1": 1,
"element_2": 1,
"elements":[
"ele_1", "ele_2", "ele_3", "ele_4"
]
},
{
"element_1":2,
"element_2":2,
"elements":[
"ele_5", "ele_6", "ele_7", "ele_8"
]
},
{
"element_1": 3,
"element_2": 3,
"elements": [
"ele_9", "ele_10", "ele_11", "ele_12"
]
}
Over here I wanted to query out the document having the element ele_1 in the elements field so that on using the java command
Query query = new Query("Required Criteria");
the document which should get returned should be
{
"element_1": 1,
"element_2": 1,
"elements":[
"ele_1", "ele_2", "ele_3", "ele_4"
]
}
I would like to mention again that the arrays in the field "elements" have no field name hence providing a key parameter while building the Criteria object is not possible. How to get the required result?
you can simply write :
Query query = new Query("{'elements' : 'ele_1'}");
You dont need $elemMatch
I need to compare two json strings by ignore some fields
I am currently using JSONAssert from org.SkyScreamer to do the comparison but does not ignore any attributes.
Json 1 :
{
"contributions": [
[
{
"order" : 1,
"contributorId" : "1980"
}
]
]
}
Json 2 :
{
"contributions": [
[
{
"order": 1,
"contributorId" : "5789"
}
]
]
}
ArrayValueMatcher<Object> arrValMatch1 = new ArrayValueMatcher<>(new CustomComparator(
JSONCompareMode.NON_EXTENSIBLE,
new Customization("contributions[0][*].contributorId",(o1, o2) -> true)));
Customization arrayValueMatchCustomization = new Customization("contributions", arrValMatch1);
CustomComparator customArrayValueComparator = new CustomComparator(
JSONCompareMode.NON_EXTENSIBLE,
arrayValueMatchCustomization);
assertEquals(subJson1, json2, customArrayValueComparator);
I expect the above scenario should be passed. But its failing with
Exception in thread "main" java.lang.AssertionError: contributions[0][contributorId=1980]
Expected: a JSON object
but none found
; contributions[0][contributorId=5789]
Unexpected: a JSON object
Use *.contributorId instead of contributions[0][*].contributorId
I have a String as below stored in datasets_detail column of table item_action_info :
[{
"datasetAttributeId": 5,
"nextUpdate": 1587546084,
"lastSuccessfulRefreshTime": 1587546084,
"refreshActionId": 2,
"errorCode": 0,
"isMFAInstanceRefreshRequired": false
}, {
"datasetAttributeId": 6,
"nextUpdate": 1587546084,
"lastSuccessfulRefreshTime": 1587546084,
"refreshActionId": 2,
"errorCode": 0,
"isMFAInstanceRefreshRequired": false
}, {
"datasetAttributeId": 8,
"nextUpdate": 1587546084,
"lastSuccessfulRefreshTime": 1587546084,
"refreshActionId": 2,
"errorCode": 0,
"isMFAInstanceRefreshRequired": false
}]
I want replace value for each occurrences of "lastSuccessfulRefreshTime".
How to i do in Java?
Below is sample piece of code. Please advice in completing the same.
String strJson =CommonUtils.clobStringConversion(resultSet.getClob("DATASETS_DETAILS"));
// complete the rest code here
If you want to manipulate it as a JSON object, then you can do it via Google GSON.
String strJson =CommonUtils.clobStringConversion(resultSet.getClob("DATASETS_DETAILS"));
Gson gson = new Gson();
List<StringMap> parsedObject = new Gson().fromJson(strJson, List.class);
// Final String
String newReplacedString = gson.toJson(parsedObject.stream().map((item) -> {
// Put your new value here
item.put("lastSuccessfulRefreshTime", "Replaced Value");
return item;
}).collect(Collectors.toList()));