Update elements from json string java - java

i have following json string response received from the server.
{
"entities":[
{
"Fields":[
{
"Name":"detection-version",
"values":[
{
}
]
},
{
"Name":"subject",
"values":[
]
},
{
"Name":"description",
"values":[
{
}
]
},
{
"Name":"target-rcyc",
"values":[
{
}
]
},
{
"Name":"project",
"values":[
{
}
]
},
{
"Name":"changeset",
"values":[
{
}
]
},
{
"Name":"has-linkage",
"values":[
{
"value":"N"
}
]
},
{
"Name":"last-modified",
"values":[
{
"value":"2016-05-31 18:38:54"
}
]
},
{
"Name":"has-others-linkage",
"values":[
{
"value":"N"
}
]
},
{
"Name":"attachment",
"values":[
{
}
]
},
{
"Name":"cycle-id",
"values":[
]
},
{
"Name":"request-type",
"values":[
{
}
]
},
{
"Name":"build-detected",
"values":[
{
}
]
},
{
"Name":"creation-time",
"values":[
{
"value":"2016-05-17"
}
]
},
{
"Name":"actual-fix-time",
"values":[
]
},
{
"Name":"id",
"values":[
{
"value":"4"
}
]
},
{
"Name":"run-reference",
"values":[
]
},
{
"Name":"request-note",
"values":[
{
}
]
},
{
"Name":"request-server",
"values":[
{
}
]
},
{
"Name":"severity",
"values":[
{
"value":"1-Low"
}
]
},
{
"Name":"to-mail",
"values":[
{
}
]
},
{
"Name":"owner",
"values":[
{
}
]
},
{
"Name":"detected-by",
"values":[
{
"value":"demouser02"
}
]
},
{
"Name":"build-closed",
"values":[
{
}
]
},
{
"Name":"step-reference",
"values":[
]
},
{
"Name":"estimated-fix-time",
"values":[
]
},
{
"Name":"reproducible",
"values":[
{
}
]
},
{
"Name":"ver-stamp",
"values":[
{
"value":"1"
}
]
},
{
"Name":"request-id",
"values":[
]
},
{
"Name":"priority",
"values":[
{
}
]
},
{
"Name":"cycle-reference",
"values":[
{
}
]
},
{
"Name":"environment",
"values":[
{
}
]
},
{
"Name":"target-rel",
"values":[
{
}
]
},
{
"Name":"test-reference",
"values":[
]
},
{
"Name":"planned-closing-ver",
"values":[
{
}
]
},
{
"Name":"extended-reference",
"values":[
{
}
]
},
{
"Name":"dev-comments",
"values":[
{
}
]
},
{
"Name":"detected-in-rcyc",
"values":[
{
}
]
},
{
"Name":"closing-version",
"values":[
{
}
]
},
{
"Name":"name",
"values":[
{
"value":"test"
}
]
},
{
"Name":"has-change",
"values":[
{
}
]
},
{
"Name":"user-01",
"values":[
{
}
]
},
{
"Name":"has-changeset-link",
"values":[
{
"value":"N"
}
]
},
{
"Name":"detected-in-rel",
"values":[
{
}
]
},
{
"Name":"status",
"values":[
{
}
]
},
{
"Name":"closing-date",
"values":[
]
}
],
"Type":"defect",
"children-count":0
},
{
"Fields":[
{
"Name":"detection-version",
"values":[
{
}
]
},
{
"Name":"subject",
"values":[
]
},
{
"Name":"description",
"values":[
{
}
]
},
{
"Name":"target-rcyc",
"values":[
{
}
]
},
{
"Name":"project",
"values":[
{
}
]
},
{
"Name":"changeset",
"values":[
{
}
]
},
{
"Name":"has-linkage",
"values":[
{
"value":"N"
}
]
},
{
"Name":"last-modified",
"values":[
{
"value":"2016-05-31 18:38:58"
}
]
},
{
"Name":"has-others-linkage",
"values":[
{
"value":"N"
}
]
},
{
"Name":"attachment",
"values":[
{
}
]
},
{
"Name":"cycle-id",
"values":[
]
},
{
"Name":"request-type",
"values":[
{
}
]
},
{
"Name":"build-detected",
"values":[
{
}
]
},
{
"Name":"creation-time",
"values":[
{
"value":"2016-05-17"
}
]
},
{
"Name":"actual-fix-time",
"values":[
]
},
{
"Name":"id",
"values":[
{
"value":"5"
}
]
},
{
"Name":"run-reference",
"values":[
]
},
{
"Name":"request-note",
"values":[
{
}
]
},
{
"Name":"request-server",
"values":[
{
}
]
},
{
"Name":"severity",
"values":[
{
"value":"1-Low"
}
]
},
{
"Name":"to-mail",
"values":[
{
}
]
},
{
"Name":"owner",
"values":[
{
}
]
},
{
"Name":"detected-by",
"values":[
{
"value":"demouser02"
}
]
},
{
"Name":"build-closed",
"values":[
{
}
]
},
{
"Name":"step-reference",
"values":[
]
},
{
"Name":"estimated-fix-time",
"values":[
]
},
{
"Name":"reproducible",
"values":[
{
}
]
},
{
"Name":"ver-stamp",
"values":[
{
"value":"1"
}
]
},
{
"Name":"request-id",
"values":[
]
},
{
"Name":"priority",
"values":[
{
}
]
},
{
"Name":"cycle-reference",
"values":[
{
}
]
},
{
"Name":"environment",
"values":[
{
}
]
},
{
"Name":"target-rel",
"values":[
{
}
]
},
{
"Name":"test-reference",
"values":[
]
},
{
"Name":"planned-closing-ver",
"values":[
{
}
]
},
{
"Name":"extended-reference",
"values":[
{
}
]
},
{
"Name":"dev-comments",
"values":[
{
}
]
},
{
"Name":"detected-in-rcyc",
"values":[
{
}
]
},
{
"Name":"closing-version",
"values":[
{
}
]
},
{
"Name":"name",
"values":[
{
"value":"test"
}
]
},
{
"Name":"has-change",
"values":[
{
}
]
},
{
"Name":"user-01",
"values":[
{
}
]
},
{
"Name":"has-changeset-link",
"values":[
{
"value":"N"
}
]
},
{
"Name":"detected-in-rel",
"values":[
{
}
]
},
{
"Name":"status",
"values":[
{
}
]
},
{
"Name":"closing-date",
"values":[
]
}
],
"Type":"defect",
"children-count":0
}
],
"TotalResults":920
}
i need to update the following values for all defects in this string
{
"Name":"detected-by",
"values":[
{
"value":"demouser02"
}
]
}
i need to update detected by to "Reported by" and demouser02 to "Adwait"
Please help.

JSONSimple. AS Simple as it gets. Try it! https://www.mkyong.com/java/json-simple-example-read-and-write-json/

If I understood correctly what you mean, you can do somthing like this :
public class EntiteFile extends Serializable{
private List<Entite<Filed>> entites;
....
}
public class Entite<Filed> extends Serializable {
private List<Filed> fileds;
}
public clas Value extends Serializable {
private String value;
....
}
public class Filed extends Serializable {
private String name;
private List<Value> values ;
.....
}
and to read you're file you can use :
EntiteFile entities = mapper.readValue(getJsonFile(nameFile), EntiteFile.class);
for(Entite entite : entities.getEntites ){
for(Field field : Fields ){
if("detected-by".equals(field.getName())){
field.setName("Reported by");
for(Value value : field.getValues){
value.setValue("Adwait");
}
}
}
}
...
...

Related

Not able to print all non empty values in nested json using java

My purpose it to print Json only with non empty objects. As in attached picture i have empty objects which i don't want to print them. It doesn't matter which library to choose either Gson or Jackson , etc'
Tried so far a lot of combination but in didn't worked
My data structure is :
Map<AWSRegion, List<SecurityGroupDiff>> listEntry...
public class SecurityGroupDiff {
#JsonInclude(Include.NON_EMPTY) ... // on all fields/objects
private String groupId;
private String groupName;
private String vpcId;
private String owner;
private String type;
private List<IPPermissionDiff> ipPermissionDiffs;
}
Tryid so far:
private static final ObjectMapper MAPPER = new ObjectMapper().
setSerializationInclusion(Include.NON_NULL).setSerializationInclusion(Include.NON_EMPTY).setVisibility(PropertyAccessor.FIELD, JsonAutoDetect.Visibility.ANY);
MAPPER.writerWithDefaultPrettyPrinter()
.writeValueAsString(listEntry)
the result is still the same ...
{
"CANADA" : [ { }, { }, { }, { }, {
"ipPermissionDiffs" : [ { }, { }, { }, { } ]
}, { }, { }, { } ],
"ASIA_SIDNEY" : [ {
"ipPermissionDiffs" : [ { }, { }, { }, { }, { } ]
}, { }, { }, { }, { }, { }, { }, {
"ipPermissionDiffs" : [ { }, { }, { }, { } ]
}, { } ],
"EU" : [ { }, { }, { }, { }, { }, { }, { }, { }, {
"ipPermissionDiffs" : [ { }, { }, { }, { }, { }, { }, { } ]
}, { } ]
}
thanks

Java Jolt bazaarvoice nested array

I am using Jolt to transform data from
{
"Data": {
"ROOT": {
"MODIFIED_DATE": "2018-06-27T13:53:47.8",
"A1": [
{
"FLD1": "BB",
"A2": [
{
"FLD2": 1
}
]
},
{
"FLD1": "AA",
"A2": [
{
"FLD2": 2
}
]
}
]
}
},
"metaData": {
"FLD3": "5f3c4"
}
}
To
{
"modifiedDate": "2018-06-27T13:53:47.8",
"a1": [
{
"fld1": "BB",
"a2": [
{
"fld2": 1
}
]
},
{
"fld1": "AA",
"a2": [
{
"fld2": 2
}
]
}
],
fld3: "5f3c4"
}
My spec is
[
{
"operation": "shift",
"spec": {
"Data": {
"ROOT": {
"MODIFIED_DATE": "modifiedDate",
"A1": {
"*": {
"FLD1": "a1[&1]",
"A2": {
"*": {
"FLD2": "a2[&2].fld2"
}
}
}
}
}
},
"metaData": {
"FLD3": "fld3"
}
}
},
{
"operation": "default",
"spec": {}
}
]
But it doesn't work properly. What have i missed?
Figured it out.
[
{
"operation": "shift",
"spec": {
"Data": {
"ROOT": {
"MODIFIED_DATE": "modifiedDate",
"A1": {
"*": {
"FLD1": "a1[&1].fld1",
"A2": {
"*": {
"FLD2": "a1[&3].a2[&1].fld2"
}
}
}
}
}
},
"metaData": {
"FLD3": "fld3"
}
}
},
{
"operation": "default",
"spec": {}
}
]

Java Driver representation of Mongodb aggregation

I have the following aggregation which Im not able to figure out how to represent in Java/Springboot mongodb driver.
db.metric.aggregate([
{ $match: { "key.runid":1} },
{ "$project": {
"data": {
"$filter": {
"input": { "$objectToArray": "$$ROOT.metricData" },
"cond": { "$ne": [ "$$this.k", "_id" ] }
}
}
}},
{ "$unwind": "$data" },
{ "$group": {
"_id": "$data.k",
"v": { "$avg": "$data.v" }
}},
{ "$sort": { "_id": 1 } },
{ "$group": {
"_id": null,
"data": { "$push": { "k": "$_id", "v": "$v" } }
}},
{ "$replaceRoot": {
"newRoot": { "$arrayToObject": "$data" }
}}
])
I have attempted the following but being new to this Im not able to figure out how to write the filter for the data field :
Aggregation aggregation = newAggregation(
match(getCriteriaForMetricKey(keys)),
project("data", ""));
Can someone help me out.

Mongo string query to MongoTemplate (java, springframework)

i have query
db.getCollection('collectionName').aggregate([
{
$project: {
'_id':1,
'arrayField': {
$filter: {
input: '$arrayField',
as: 'arrayField',
cond: { $or: [
{$eq: [ [LUUID("********-****-****-****-************")],['$$arrayField._id'] ] },
{$eq: [ [LUUID("********-****-****-****-************")],['$$arrayField._id'] ] }
] }
}
}
}
}])
How will it look in MongoTemplate? And it is possible to rewrite the cond?
cond rewritten as:
db.getCollection('collectionName').aggregate([
{
$project: {
'_id':1,
'arrayField': {
$filter: {
input: '$arrayField',
as: 'arrayField',
cond:
{ "$setIsSubset": [
{ "$map": {
"input": ["$$arrayField._id"],
"as": "el",
"in": "$$el"
}},
[ LUUID("********-****-****-****-************") ,LUUID("********-****-****-****-************")],
]}
}
}
}
}])

How to print the full elasticsearch request for debug in java

I use
ElasticSearchTemplate().queryForPage(SearchQuery, CLASS)
How can I print the full json request?
I manage to print only filter by doing :
searchQuery.getFilter().toString()
But cant manage to do the same with:
searchQuery.getAggregations().toString();
I would like to print in console something like :
"aggs": {
"agg1": {
"terms": {
"field": "basket_id_1",
"size": 0
},
"aggs": {
"basket_id_2": {
"terms": {
"field": "basket_id_2",
"size": 0
},
"aggs": {
"basket_id_3": {
"terms": {
"field": "basket_id_3",
"size": 0
}
}
}
}
}
}
}
This is what I've started using to do the same thing.
{
"top_agg": {
"terms": {
"field": "id",
"size": 100
},
"aggregations": {
"parent": {
"nested": {
"path": "transactions"
},
"aggregations": {
"totals": {
"filter": {
"terms": {
"transactions.type": [
"ttype"
]
}
},
"total_events": {
"cardinality": {
"field": "parent.field"
}
}
}
}
}
}
}
}
NativeSearchQuery query = queryBuilder.build();
if (query.getQuery() != null) {
log.debug(query.getQuery().toString());
}
if (query.getAggregations() != null) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.startObject();
for (AbstractAggregationBuilder subAgg : query.getAggregations()) {
subAgg.toXContent(builder, ToXContent.EMPTY_PARAMS);
}
builder.endObject();
log.debug(builder.string());
} catch (IOException e) {
log.debug("Error parsing aggs");
}
}
Could you use the SearchResponse.getAggregations().asList() ?

Categories

Resources