Related
I am trying to generate query dynamically based on the inputs but in the generated query i can see there are only two aggregations are getting generated how can i make each fields to have the separate aggregations below is the code what i have tried and the response what i'm getting.
From main() i'm calling
buildSearchCriteria("1");
Here i am setting the aggregation type and respective values:
public static void buildSearchCriteria(String... exceptionId) {
SearchCriteria searchCriteria = new SearchCriteria();
Map<String, List<FieldNameAndPath>> stringListMap = new HashMap<>();
stringListMap.put("nested", asList(new FieldNameAndPath("nested", "recommendations",
"recommendations", null, emptyList(), 1)));
stringListMap.put("filter", asList(new FieldNameAndPath("filter", "exceptionIds", "recommendations.exceptionId.keyword",
asList(exceptionId),
asList(new NestedAggsFields("terms", "exceptionIdsMatch")), 2)));
stringListMap.put("terms", asList(new FieldNameAndPath("terms", "by_exceptionId", "recommendations.exceptionId.keyword", null, emptyList(), 3),
new FieldNameAndPath("terms", "by_item", "recommendations.item.keyword", null, emptyList(), 4),
new FieldNameAndPath("terms", "by_destination", "recommendations.location.keyword", null, emptyList(), 5),
new FieldNameAndPath("terms", "by_trans", "recommendations.transportMode.keyword", null, emptyList(), 6),
new FieldNameAndPath("terms", "by_sourcelocation", "recommendations.sourceLocation.keyword", null, emptyList(), 7),
new FieldNameAndPath("terms", "by_shipdate", "recommendations.shipDate", null, emptyList(), 8),
new FieldNameAndPath("terms", "by_arrival", "recommendations.arrivalDate", null, emptyList(), 9)));
stringListMap.put("sum", asList(new FieldNameAndPath("sum", "quantity", "recommendations.transferQuantity", null, emptyList(), 10),
new FieldNameAndPath("sum", "transfercost", "recommendations.transferCost", null, emptyList(), 11),
new FieldNameAndPath("sum", "revenueRecovered", "recommendations.revenueRecovered", null, emptyList(), 12)));
System.out.println(stringListMap);
searchCriteria.setStringListMap(stringListMap);
aggregate(searchCriteria);
}
Below is the aggregate function which will get the the above information and builds query:
public static void aggregate(SearchCriteria searchCriteria) throws IOException {
Map<String, List<FieldNameAndPath>> map = searchCriteria.getStringListMap();
List<FieldNameAndPath> nesteds = map.get("nested");
List<FieldNameAndPath> filter = map.get("filter");
List<FieldNameAndPath> terms = map.get("terms");
List<FieldNameAndPath> sums = map.get("sum");
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = new SamplerAggregationBuilder("parent");
nesteds.stream().forEach(l -> buildAggregations(l, aggregationBuilder));
filter.stream().forEach(l -> buildAggregations(l, aggregationBuilder));
terms.stream().forEach(l -> buildAggregations(l, aggregationBuilder));
sums.stream().forEach(l -> buildAggregations(l, aggregationBuilder));
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("index");
searchRequest.types("type");
sourceBuilder.aggregation(aggregationBuilder);
searchRequest.source(sourceBuilder);
System.out.println(searchRequest.source().toString());
}
buildAggregations method:
private static AggregationBuilder buildAggregations(FieldNameAndPath fieldNameAndPath , AggregationBuilder parentAggregationBuilder) {
if(fieldNameAndPath.getAggType().equals("nested")){
parentAggregationBuilder = AggregationBuilders.nested(fieldNameAndPath.getFieldName(), fieldNameAndPath.getFieldPath());
}
if(fieldNameAndPath.getAggType().equals("filter")){
parentAggregationBuilder.subAggregation(AggregationBuilders
.filter(fieldNameAndPath.getFieldName(),
QueryBuilders.termsQuery(fieldNameAndPath.getNestedAggs()
.stream().map(nestedAggsFields -> nestedAggsFields.getFieldName()).findFirst().get(), fieldNameAndPath.getFieldValues())));
}
if(fieldNameAndPath.getAggType().equals("terms")){
parentAggregationBuilder.subAggregation(AggregationBuilders.terms(fieldNameAndPath.getFieldName())
.field(fieldNameAndPath.getFieldPath()));
}
if(fieldNameAndPath.getAggType().equals("sum")){
parentAggregationBuilder.subAggregation(AggregationBuilders.
sum(fieldNameAndPath.getFieldName()).field(fieldNameAndPath.getFieldPath()));
}
return parentAggregationBuilder;
}
SearchCriteria class:
#Data
public class SearchCriteria {
Map<String, List<FieldNameAndPath>> stringListMap;
private List<String> searchFields;
}
And the DTO FieldNameAndPath:
public class FieldNameAndPath{
private String aggType;
private String fieldName;
private String fieldPath;
private List<String> fieldValues;
private List<NestedAggsFields> nestedAggs;
private int order;
}
And the query output from the above code is:
{
"aggregations": {
"parent": {
"sampler": {
"shard_size": 100
},
"aggregations": {
"exceptionIds": {
"filter": {
"terms": {
"exceptionIdsMatch": [
"1"
],
"boost": 1
}
}
},
"by_exceptionId": {
"terms": {
"field": "recommendations.exceptionId.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
},
"by_item": {
"terms": {
"field": "recommendations.item.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
},
"by_destination": {
"terms": {
"field": "recommendations.location.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
},
"by_trans": {
"terms": {
"field": "recommendations.transportMode.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
},
"by_sourcelocation": {
"terms": {
"field": "recommendations.sourceLocation.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
},
"by_shipdate": {
"terms": {
"field": "recommendations.shipDate",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
},
"by_arrival": {
"terms": {
"field": "recommendations.arrivalDate",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
}
},
"quantity": {
"sum": {
"field": "recommendations.transferQuantity"
}
},
"transfercost": {
"sum": {
"field": "recommendations.transferCost"
}
},
"revenueRecovered": {
"sum": {
"field": "recommendations.revenueRecovered"
}
}
}
}
}
}
Expected Query is:
{
"size": 0,
"aggregations": {
"exceptionIds": {
"nested": {
"path": "recommendations"
},
"aggregations": {
"exceptionIdsMatch": {
"filter": {
"terms": {
"recommendations.exceptionId.keyword": [
"1"
],
"boost": 1
}
},
"aggregations": {
"by_exceptionId": {
"terms": {
"field": "recommendations.exceptionId.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_item": {
"terms": {
"field": "recommendations.item.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_destination": {
"terms": {
"field": "recommendations.location.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_trans": {
"terms": {
"field": "recommendations.transportMode.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_sourcelocation": {
"terms": {
"field": "recommendations.sourceLocation.keyword",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_shipdate": {
"terms": {
"field": "recommendations.shipDate",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"by_arrival": {
"terms": {
"field": "recommendations.arrivalDate",
"size": 10,
"min_doc_count": 1,
"shard_min_doc_count": 0,
"show_term_doc_count_error": false,
"order": [
{
"_count": "desc"
},
{
"_key": "asc"
}
]
},
"aggregations": {
"quantity": {
"sum": {
"field": "recommendations.transferQuantity"
}
},
"transfercost": {
"sum": {
"field": "recommendations.transferCost"
}
},
"revenueRecovered": {
"sum": {
"field": "recommendations.revenueRecovered"
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
}
I have some JSON, declared as string for my testing purposes in main method.
My goal is to use JSONPATH with DocumentContext, to override property if property exists, and to add it if it does not exists.
Is there possibility to achieve that ?
My JSON
How to add new node to Json using JsonPath?
Convert a JSON object to another JSON object in Java
I've visited these two but my result is not JSON but "com.jayway.jsonpath.internal.JsonReader#1ae369b7"
{
"meta": {
"drilldownEnabled": false
},
"chart": {
renderTo:"container",
"additionalData": {
"dateTime": false,
"datetype": "string",
"cliccable": true,
"drillable": false,
"drillableChart": false,
"isCockpit": true,
"categoryColumn": "periodo_analisi",
"categoryGroupBy": "",
"categoryGroupByNamens": "",
"categoryName": "periodo_analisi",
"categoryOrderColumn": "",
"categoryOrderType": "",
"categoryStacked": "",
"categoryStackedType": ""
},
"zoomType": "xy",
"panning": true,
"type": "column",
"options3d": {
"enabled": false,
"alpha": 25,
"beta": 15,
"depth": 50,
"viewDistance": 25
},
"backgroundColor": "#FFFFFF",
"heightDimType": "pixels",
"widthDimType": "pixels",
"plotBackgroundColor": null,
"plotBorderWidth": null,
"plotShadow": false,
"borderColor": "#FFFFFF",
"style": {
"backgroundColor": "#FFFFFF",
"fontFamily": "",
"fontWeight": "",
"fontSize": ""
},
"events": {}
},
"colors": [
"#ff5722"
],
"title": {
"text": "",
"style": {
"align": "",
"color": "",
"fontFamily": "",
"fontSize": "",
"fontWeight": ""
}
},
"legend": {
"enabled": false
},
"xAxis": [
{
"plotBands": [
{
"label": {
"text": "",
"align": "center"
},
"color": "",
"from": 0,
"to": 0
}
],
"plotLines": [
{
"label": {
"text": "",
"align": "center"
},
"color": "",
"dashStyle": "",
"value": 0,
"width": 0
}
],
"type": "category",
"id": 0,
"title": {
"customTitle": false,
"text": "periodo_analisi",
"style": {}
},
"labels": {
"style": {
"color": "",
"fontFamily": "",
"fontSize": "",
"fontWeight": ""
},
"align": ""
}
}
],
"yAxis": [
{
"plotBands": [
{
"label": {
"text": "",
"align": "center"
},
"color": "",
"from": 0,
"to": 0
}
],
"plotLines": [
{
"label": {
"text": "",
"align": "center"
},
"color": "",
"dashStyle": "",
"value": 0,
"width": 0,
"zIndex": 1
}
],
"title": {
"text": "NUM_GG_GIACENZA_AVG",
"customTitle": false,
"style": {
"color": "",
"fontFamily": "",
"fontWeight": "",
"fontSize": ""
}
},
"labels": {
"style": {
"color": "",
"fontFamily": "",
"fontSize": "",
"fontWeight": ""
},
"align": ""
},
"gridLineDashStyle": "$convertedTypeline",
"minorGridLineDashStyle": "$convertedMinorTpeline"
}
],
"series": [
{
"name": "NUM_GG_GIACENZA_AVG",
"dataLabels": {
"style": {
"color": "",
"fontFamily": "",
"fontWeight": "",
"fontSize": "",
"fontStyle": ""
},
"enabled": true,
"labelFormat": "{y:,.2f}"
},
"data": [
{
"drilldown": false,
"y": 32.6667,
"name": "Q1-2019",
"datetype": "string"
},
{
"drilldown": false,
"y": 29,
"name": "Q3-2018",
"datetype": "string"
},
{
"drilldown": false,
"y": 134.5,
"name": "Q4-2018",
"datetype": "string",
"color": "#F10AE8"
}
],
"selected": true,
"tooltip": {
"valueDecimals": 2,
"scaleFactor": "empty",
"ttBackColor": "#FCFFC5"
},
"yAxis": 0
}
],
"tooltip": {
"borderWidth": 0,
"borderRadius": 0,
"followTouchMove": false,
"followPointer": true,
"useHTML": true,
"backgroundColor": null,
"style": {
"padding": 0
}
},
"lang": {
"noData": ""
},
"noData": {
"style": {
"fontFamily": "",
"fontSize": "",
"color": ""
},
"position": {
"align": "center",
"verticalAlign": "middle"
}
},
"credits": {
"enabled": false
},
"plotOptions": {
"line": {
"marker": {
"symbol": "circle",
"lineWidth": 2
}
},
"column": {},
"bar": {},
"series": {
"cursor": "pointer",
"point": {
"events": {}
},
"dataLabels": {
"allowOverlap": true
},
"turboThreshold": 2000
}
}
}
I simply place this JSON in string as cc variable and write
DocumentContext doc = JsonPath.parse(cc).set(JsonPath.compile("$.meta"), "nani");
doc.json();
System.out.println(doc);
You're printing the DocumentContext instance instead of its content.
Try:
System.out.println(doc.read([Your JsonPath]));
Go here for more Info.
response:
[
{
"id": "e9299032e8a34d168def176af7d62da3",
"createdAt": "Nov 8, 2017 9:46:40 AM",
"model": {
"id": "eeed0b6733a644cea07cf4c60f87ebb7",
"name": "color",
"app_id": "main",
"created_at": "May 11, 2016 11:35:45 PM",
"model_version": {}
},
"input": {
"id": "df6eae07cd86483f811c5a2202e782eb",
"data": {
"concepts": [],
"metadata": {},
"image": {
"url": "http://www.sachinmittal.com/wp-content/uploads/2017/04/47559184-image.jpg"
}
}
},
"data": [
{
"hex": "#f59b2d",
"webSafeHex": "#ffa500",
"webSafeColorName": "Orange",
"value": 0.0605
},
{
"hex": "#3f1303",
"webSafeHex": "#000000",
"webSafeColorName": "Black",
"value": 0.2085
},
{
"hex": "#a33303",
"webSafeHex": "#8b0000",
"webSafeColorName": "DarkRed",
"value": 0.3815
},
{
"hex": "#000000",
"webSafeHex": "#000000",
"webSafeColorName": "Black",
"value": 0.34275
},
{
"hex": "#f7ce93",
"webSafeHex": "#ffdead",
"webSafeColorName": "NavajoWhite",
"value": 0.00675
}
],
"status": {}
}
]
need to parse this reponse in json. Please help me out.
You can try something like this..
try{
JSONArray array= new JSONArray(Yourresponse);
for(int i=0; i<=array.length();i++){
JSONObject jsonObject=array.getJSONObject(i);
String id= jsonObject.getString("id");
String created_at= jsonObject.getString("createdAt");
String model_id = jsonObject.getJSONObject("model").getString("id");
String app_id=jsonObject.getJSONObject("model").getString("app_id");
//So On... Depends on your requirements. It's just an idea!
}
}
catch (Exception e){
e.printStackTrace();
}
I am using retrofit2 to Make network request,I have searched over here but my bad luck i couldn't find any working solution. that's why i am putting my question here.
my JSON response is given below.
The problem is sometimes the REST API returns an Array of hour , but sometimes it is just a Object. How does one handle such a situation?
Is there an elegant way to handle a mixed array like this in Retrofit/Gson? I'm not responsible for the data coming from the API, so I don't think changing that will be an option.Any help would be appreciated.
{
"status": "success",
"data": [
{
"id": 30,
"name": "Rh.poutiqe",
"global_delay": "0",
"approved": true,
"min_order": "0.000",
"has_pickup": 0,
"address": {
"id": "35",
"name": "Store Address",
"type": "house",
"block_number": "8",
"street": "85",
"avenue": "0",
"building": "2",
"floor": "",
"apartment": "",
"directions": "",
"lat": null,
"lng": null,
"city": {
"id": "79",
"name": "Bayan",
"zone": "3",
"governate": "Hawally"
}
},
"status": "Available",
"owner": {
"id": "32",
"username": "+96550199900",
"creation_date": "2017-08-07 09:46:49",
"info": {
"name": "Asmaa alkandri",
"email": "Asooma_q8#hotmail.com",
"mobile": "50199900",
"store_id": "30",
"device_token": "e01efb2f03cd43509242c7b38ca890471db2e5b056f50b7a3661c34ab45b0b6e"
},
"addresses": [
{
"id": "35",
"name": "Store Address",
"type": "house",
"block_number": "8",
"street": "85",
"avenue": "0",
"building": "2",
"floor": "",
"apartment": "",
"directions": "",
"lat": null,
"lng": null,
"city": {
"id": "79",
"name": "Bayan",
"zone": "3",
"governate": "Hawally"
}
}
]
},
"open": false,
"image": {
"src": "https://api.bits.com.kw/assets/stores/30/y1nzl.jpg"
},
"hours": {
"2": [
{
"id": "117",
"day_id": "2",
"day_of_week": "Tuesday",
"start_hour": "1400",
"end_hour": "2300"
}
],
"3": [
{
"id": "118",
"day_id": "3",
"day_of_week": "Wednesday",
"start_hour": "1400",
"end_hour": "2300"
}
]
},
"next_available": {
"day_of_week": "Tuesday",
"start_hour": "1400",
"end_hour": "2300",
"day_id": "2",
"date": "2017-09-19"
}
},
{
"id": 57,
"name": "RH Kitchen",
"global_delay": "0",
"approved": true,
"min_order": "0.000",
"has_pickup": 0,
"address": {
"id": "63",
"name": "Store Address",
"type": "house",
"block_number": "5",
"street": "2",
"avenue": "",
"building": "97",
"floor": "",
"apartment": "",
"directions": "",
"lat": null,
"lng": null,
"city": {
"id": "79",
"name": "Bayan",
"zone": "3",
"governate": "Hawally"
}
},
"status": "Not Receiving Orders",
"owner": {
"id": "57",
"username": "+96566659454",
"creation_date": "2017-09-09 11:32:19",
"info": {
"name": "RH Kitchen",
"email": "taiba.aldarmi#gmail.com",
"mobile": "66659454",
"store_id": "57",
"device_token": "f6fffd3a393e9aea53863cffbb55b51a3afd2475e952091ea362a85fc930ec9a"
},
"addresses": [
{
"id": "63",
"name": "Store Address",
"type": "house",
"block_number": "5",
"street": "2",
"avenue": "",
"building": "97",
"floor": "",
"apartment": "",
"directions": "",
"lat": null,
"lng": null,
"city": {
"id": "79",
"name": "Bayan",
"zone": "3",
"governate": "Hawally"
}
}
]
},
"open": false,
"image": {
"src": "https://api.bits.com.kw/placeholder.jpg"
},
"hours": [],
"next_available": false
},
{
"id": 64,
"name": "Lets__shop",
"global_delay": "1440",
"approved": true,
"min_order": "5.000",
"has_pickup": 0,
"address": {
"id": "64",
"name": "Store Address",
"type": "house",
"block_number": "3",
"street": "312",
"avenue": "",
"building": "56",
"floor": "",
"apartment": "",
"directions": "",
"lat": "0.000000000000000000",
"lng": "0.000000000000000000",
"city": {
"id": "126",
"name": "Saad Al Abdullah",
"zone": "9",
"governate": "Jahra"
}
},
"status": "Available",
"owner": {
"id": "58",
"username": "+96555899184",
"creation_date": "2017-09-09 18:33:12",
"info": {
"name": "Moneera ibrahim",
"email": "Moneeera96#gmail.com",
"mobile": "55899184",
"store_id": "64",
"device_token": null
},
"addresses": [
{
"id": "64",
"name": "Store Address",
"type": "house",
"block_number": "3",
"street": "312",
"avenue": "",
"building": "56",
"floor": "",
"apartment": "",
"directions": "",
"lat": "0.000000000000000000",
"lng": "0.000000000000000000",
"city": {
"id": "126",
"name": "Saad Al Abdullah",
"zone": "9",
"governate": "Jahra"
}
}
]
},
"open": true,
"next_available": {
"day_of_week": "Today",
"start_hour": "1646",
"end_hour": "2230",
"day_id": "0",
"date": "2017-09-17 1646"
},
"image": {
"src": "https://api.bits.com.kw/assets/stores/64/0xqh4.jpg"
},
"hours": [
[
{
"id": "161",
"day_id": "0",
"day_of_week": "Sunday",
"start_hour": "730",
"end_hour": "2230"
}
],
[
{
"id": "162",
"day_id": "1",
"day_of_week": "Monday",
"start_hour": "730",
"end_hour": "2230"
}
],
[
{
"id": "163",
"day_id": "2",
"day_of_week": "Tuesday",
"start_hour": "730",
"end_hour": "2230"
}
],
[
{
"id": "164",
"day_id": "3",
"day_of_week": "Wednesday",
"start_hour": "730",
"end_hour": "2230"
}
],
[
{
"id": "165",
"day_id": "4",
"day_of_week": "Thursday",
"start_hour": "730",
"end_hour": "2230"
}
],
[
{
"id": "166",
"day_id": "5",
"day_of_week": "Friday",
"start_hour": "730",
"end_hour": "2230"
}
],
[
{
"id": "167",
"day_id": "6",
"day_of_week": "Saturday",
"start_hour": "730",
"end_hour": "2230"
}
]
]
}
]
}
I have Make POJO class like:
public class StoreModel implements Parcelable{
#SerializedName("id")
public int id;
#SerializedName("name")
public String name;
#SerializedName("global_delay")
public String global_delay;
#SerializedName("approved")
public boolean approved;
#SerializedName("min_order")
public String min_order;
#SerializedName("has_pickup")
public int has_pickup;
#SerializedName("address")
public AddressModel address;
#SerializedName("status")
public String status;
#SerializedName("owner")
public OwnerModel owner;
#SerializedName("open")
public boolean open;
#SerializedName("next_available")
public Object next_available;
#SerializedName("image")
public ImageModel image;
protected StoreModel(Parcel in) {
id = in.readInt();
name = in.readString();
global_delay = in.readString();
approved = in.readByte() != 0;
min_order = in.readString();
has_pickup = in.readInt();
address = in.readParcelable(AddressModel.class.getClassLoader());
status = in.readString();
owner = in.readParcelable(OwnerModel.class.getClassLoader());
open = in.readByte() != 0;
image = in.readParcelable(ImageModel.class.getClassLoader());
//next_available = in.readParcelable(NextAvailableModel.class.getClassLoader());
}
public static final Creator<StoreModel> CREATOR = new Creator<StoreModel>() {
#Override
public StoreModel createFromParcel(Parcel in) {
return new StoreModel(in);
}
#Override
public StoreModel[] newArray(int size) {
return new StoreModel[size];
}
};
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeInt(id);
parcel.writeString(name);
parcel.writeString(global_delay);
parcel.writeByte((byte) (approved ? 1 : 0));
parcel.writeString(min_order);
parcel.writeInt(has_pickup);
parcel.writeParcelable(address, i);
parcel.writeString(status);
parcel.writeParcelable(owner, i);
parcel.writeByte((byte) (open ? 1 : 0));
parcel.writeParcelable(image, i);
/*if(next_available instanceof NextAvailableModel)
parcel.writeParcelable((NextAvailableModel)next_available, i);
else if(next_available instanceof Boolean)
parcel.writeByte((byte) ((Boolean)next_available ? 1 : 0));*/
}
#SerializedName("hours")
#Expose
public List<List<HoursModel>> hours;
}
**And HoursModel Java Class**
public class HoursModel implements Parcelable{
#SerializedName("id")
public String id;
#SerializedName("day_id")
public String day_id;
#SerializedName("day_of_week")
public String day_of_week;
#SerializedName("start_hour")
public String start_hour;
#SerializedName("end_hour")
public String end_hour;
protected HoursModel(Parcel in) {
id = in.readString();
day_id = in.readString();
day_of_week = in.readString();
start_hour = in.readString();
end_hour = in.readString();
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(day_id);
dest.writeString(day_of_week);
dest.writeString(start_hour);
dest.writeString(end_hour);
}
#Override
public int describeContents() {
return 0;
}
public static final Creator<HoursModel> CREATOR = new Creator<HoursModel>() {
#Override
public HoursModel createFromParcel(Parcel in) {
return new HoursModel(in);
}
#Override
public HoursModel[] newArray(int size) {
return new HoursModel[size];
}
};
}
First, if possible, verify if you can fix this braindead API. :)
First of all, notice that the API returns 2 very different types of data:
list of hours (no keys)
map of hours (each element has a key!)
You'll need to represent this structure in your POJO somehow. Maybe a list of key-hour pairs, like List<Pair<String, Hour>, with nullable key? Your call.
Second, you need to create a custom TypeAdapter that can deserialize a list and/or map into a Java object.
Once you have a type adapter, you can either attach it to a field using #JsonAdapter annotation, or register custom type in Gson builder.
https://google.github.io/gson/apidocs/com/google/gson/annotations/JsonAdapter.html
http://www.javacreed.com/gson-typeadapter-example/
I am a novice in Java and I am looking for a way to flatten json documents.
I have tried Object mapper but without success and I have also tried to do with json node but still get no success .
I found this link but the results is not what I need :https://github.com/wnameless/json-flattener
I have also been helped before but the example was too specific and I cannot do the same things because the documents is too long this is why I am looking for a way to make it generic: Flatten json documents in Java
I need to transform "any" json documents like in the example below :
Here is an example of my documents
Documents recieved:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 10,
"max_score": 0,
"hits": []
},
"aggregations": {
"groupe": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "a",
"doc_count": 1,
"date": {
"buckets": [
{
"key_as_string": "2017-05-03T00:00:00.000Z",
"key": 1493769600000,
"doc_count": 1,
"value": {
"value": 1
}
},
{
"key_as_string": "2017-05-03T01:00:00.000Z",
"key": 1493776800000,
"doc_count": 1,
"value": {
"value": 3
}
}
]
}
},
{
"key": "b",
"doc_count": 4,
"date": {
"buckets": [
{
"key_as_string": "2017-05-03T00:00:00.000Z",
"key": 1493769600000,
"doc_count": 1,
"value": {
"value": 4
}
},
{
"key_as_string": "2017-05-03T01:00:00.000Z",
"key": 1493773200000,
"doc_count": 1,
"value": {
"value": 3
}
}
]
}
}
]
}
}
}
Document Transformed:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 10,
"max_score": 0,
"hits": []
},
"aggregations": {
"groupe": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "a",
"doc_count": 1,
"date": {
"buckets": [
{
"key_as_string": "2017-05-03T00:00:00.000Z",
"key": 1493769600000,
"doc_count": 1,
"value": {
"value": 1
}
}
]
}
},
{
"key": "a",
"doc_count": 1,
"date": {
"buckets": [
{
"key_as_string": "2017-05-03T02:00:00.000Z",
"key": 1493776800000,
"doc_count": 1,
"value": {
"value": 3
}
}
]
}
},
{
"key": "b",
"doc_count": 1,
"date": {
"buckets": [
{
"key_as_string": "2017-05-03T02:00:00.000Z",
"key": 1493776800000,
"doc_count": 1,
"value": {
"value": 4
}
}
]
}
},
"key": "b",
"doc_count": 1,
"date": {
"buckets": [
{
"key_as_string": "2017-05-03T02:00:00.000Z",
"key": 1493776800000,
"doc_count": 1,
"value": {
"value": 4
}
}
]
}
}
]
}
}
}