My application uses Apace CXF for exposing REST APIs and uses jackson for marshalling and unmarshalling.. In one of the endpoint, we return a wrapper which is a Map. Displaying decimal places is crucial for this application.
#XmlRootElement(name = "Output")
class Wrapper {
private Map<String, CountVO> data;
//constructor
//getter, setter
}
class CountVO {
private BigDecimal value;
//getter, setter, constructor
//updateMethod
public void updateValue(String userDecimalFormat){
switch(userDecimalFormat){
case "1":
this.value = BigDecimal.ZERO.setScale(1);
break;
case "2":
this.value = BigDecimal.ZERO.setScale(2);
break;
case "3":
this.value= BigDecimal.ZERO.setScale(3);
break;
}
}
}
Here, default value is set to 0 with decimal points based on certain configuration.
I have added tostring and loggers at relevant places in the code. What I could see is that, when the userDecimalFormat is 1 or 2 or 3, I get the desired output, viz count is set to 0.0, 0.00 or 0.000 resp and the same is printed in the logs.
But, the moment it is converted to json format, I get the desired output only when userDecimalFormat is 2,3. When it is 1, I get it as 0 and not as 0.0 in the resultant json. Here is the snippet.
{
"Output": {
"data": {
"entry": [
{
"key": "1",
"value": {
"count": 0
}
},
{
"key": "2",
"value": {
"count": 0
}
}
]
}
}
}
In other cases, it is as follows.
{
"Output": {
"data": {
"entry": [
{
"key": "1",
"value": {
"count": "0.00"
}
},
{
"key": "2",
"value": {
"count": "0.00"
}
}
]
}
}
}
How do you suggest I resolve this issue?
Related
If there are age and name fields
When I look up here, if the name is A, the age is 30 or more, and the others are 20 or more. In this way, I want to give different conditions depending on the field value.
Does es provide this function? I would like to know which keywords to use.
You may or may not be able to tell us how to use it with QueryBuilders provided by Spring.
Thank you.
select * from people
where (name = 'a' and age >=30) or age >=20
This site can convert sql to esdsl
try this
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match_phrase": {
"name": {
"query": "a"
}
}
},
{
"range": {
"age": {
"from": "30"
}
}
}
]
}
},
{
"range": {
"age": {
"from": "20"
}
}
}
]
}
},
"from": 0,
"size": 1
}
I need to create an IDs lists for all objects that have identical (same value and quantity) parameters. I am looking for a solution that will be more efficient than two nested loops and an if.
Object structure in the dataset:
case class MergedProduct(id: String,
products: List[Product])
case class Product(productUrl: String, productId: String)
Example of data in dataset:
[ {
"id": "ID1",
"products": [
{
"product": {
"productUrl": "SOMEURL",
"productId": "1"
}
},
{
"product": {
"productUrl": "SOMEOTHERURL",
"productId": "1"
}
}
],
},
{
"id": "ID2",
"products": [
{
"product": {
"productUrl": "SOMEURL",
"productId": "1"
}
},
{
"product": {
"productUrl": "SOMEOTHERURL",
"productId": "1"
}
}
],
},
{
"id": "ID3",
"products": [
{
"product": {
"productUrl": "DIFFERENTURL",
"productId": "1"
}
},
{
"product": {
"productUrl": "SOMEOTHERURL",
"productId": "1"
}
}
],
},
{
"id": "ID4",
"products": [
{
"product": {
"productUrl": "SOMEOTHERURL",
"productId": "1"
}
},
{
"product": {
"productUrl": "DIFFERENTURL",
"productId": "1"
}
}
],
},
{
"id": "ID5",
"products": [
{
"product": {
"productUrl": "NOTDUPLICATEDURL",
"productId": "1"
}
},
{
"product": {
"productUrl": "DIFFERENTURL",
"productId": "1"
}
}
],
}
]
In this example, we have 4 objects that are duplicated, so I would like to get their ID in the corresponding lists.
Example output is List[List[String]]:
List(List("ID1", "ID2"), List("ID3","ID4"))
I am looking for something efficient and readable - the dataset we are talking about has nearly 700 million objects.
As I can remove the listed duplicates from the dataset (it does not affect the database) because the goal is one - logging them exists, so I was thinking about the solution of taking MergedProduct one by one, searching for other MergedProduct with identical Products, getting their ID, logging in they exist and then remove the mentioned MergedProduct ID from the dataset and move on to the next one until I check the whole dataset but in this case I would have to collect it first as a list of MergedProducts and then do all operations - seems like going around
After trying some options and looking for neat solutions- I think this is kinda ok:
private def getDuplicates(mergedProducts: List[MergedProduct]): List[List[String]] = {
val duplicates = mergedProducts.groupBy(_.products.sortBy(_.product.productId)).filter(_._2.size > 1).values.toList
duplicates.map(duplicates => duplicates.map(_.id))
}
I have this Json and i can´t recover the field "entidad" and "oficina":
{
"resultado": [
{
"columa": [
"p"
],
"datos": [
{
"row": [
{
"oficina": "0000",
"entidad": "1234",
"nombre": "nombre persona"
}
],
"meta": [
{
"id": 4700925,
"type": "node",
"deleted": false
}
]
}
]
}
],
"errors": [],
"responseTime": 84
}
How can I recover the field "oficina" and "entidad"?
I could use Gson or Jackson.
I can´t recover this fields.
thank you
Get your array 'row'. When you got it, you can iterate it and extract the elements:
Get resultado --> get datos --> get row, then:
for(int i=0; i<arrayJSON.length; i++) {
JSONObject objectJSON= arrayJSON.get(i);
String entidad = objectJSON.getString("entidad");
String oficina = objectJSON.getString("oficina");
}
I'm having some trouble trying query a JSON with Java JsonPath.
I have a large json with informations about people location (thousand of lines). The field to find the person location is your macAdreess. The macAdreess does not repeat, in other words when I query for a specific macAdreess, I got once result.
This is a small piece of my Json:
{
"readings": [
{
"value": {
"floorRefId": "-4564288095083560912",
"x": 86.405304,
"y": 64.4601,
"z": 0
},
"tags": {
"macAddress": "f8:e0:79:82:95:92"
},
"timestamp": 1494620148598
},
{
"value": {
"floorRefId": "-4564288095083560912",
"x": 86.540474,
"y": 64.12458,
"z": 0
},
"tags": {
"macAddress": "f4:f5:24:96:d5:cd"
},
"timestamp": 1494620148598
},
{
"value": {
"floorRefId": "-4564288095083560912",
"x": 86.31584,
"y": 64.410446,
"z": 0
},
"tags": {
"macAddress": "f4:f5:24:2a:9c:13"
},
"timestamp": 1494620148598
},
],
"gateway_uuid": "cccccccc-9f70-4d93-94be-2fa7e15ef292",
"status": "running"
}
I will need to query for one macAddress into JSON every five seconds, so if I need to traverse all JSON content, I will have a performance trouble.
Then I'm trying to use Java JsonPath API to query the macAdreess and it's running ok. But I need to get the parent node to get the location fields.
With this code:
public static void main(String[] args) throws Exception {
String json = getJsonAsString();
List<Map<String, Object>> expensive = JsonPath.parse(json).read("$..[?(#.macAddress=='f4:f5:24:96:d5:cd')]");
System.out.println(expensive);
}
I got thit result:
[{"macAddress":"f4:f5:24:96:d5:cd"}]
You need to query from the parent, and don't use the $.. deep scan.
$.readings[?(#.tags.macAddress=="f4:f5:24:96:d5:cd")]
But if you know there's only one, you'd be better off to linearly scan the data yourself. It would be more performant
I have been breaking my head for this solution , I have JSON String as follows
{"Nodes":
[
{
"Node_id": 10023
},
{
"Node_id": 10056
},
{
"Node_id":00000
}
],
"utc":136199375611
}
how to convert it to below format
{"Nodes":
[
{
"Node_id": "10023"
},
{
"Node_id": "10056"
},
{
"Node_id":"00000"
}
],
"utc":"136199375611"
}
Now i want to encode all the integer values (which ever place in the value) with double quotes "integer value", How do i do it in Regular expression's using java Patterns and matcher class or even a substring class. ur help would be much appretiated.
EDIT
the original JSON format will be like below
{
"Nodes": [
{
"Node_id": "10023",
"count": 1
},
{
"Node_id": "10056",
"count": 2
},
{
"Node_id": "+00000",
"count": 1
},
{
"Node_id": "-00000",
"count": "6"
}
],
"utc": "136199375611",
"DeliveryTime": "Tue 23rd jun 2014 12:45 AM",
"Ifr": "2333"
}
Just a quick fix.
jsonString.replaceAll("(\\d+)","\"$1\"")
But I suggest you to use proper JSON parser or GSON library to parse it into Java Object then convert Integer values to String and finally convert back that Java Object to JSON string.
GSON Library
Convert the JSON String into Java Object using Gson#fromJson()
Convert back to the JSON String from the Java using Gson#toJson().
Sample code:
class NodesDetail{
private ArrayList<NoteId> Nodes;
private String utc;
}
class NoteId{
private String Node_id;
}
...
NodesDetail obj = new Gson().fromJson(reader, NodesDetail.class);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(obj));
output:
{
"Nodes": [
{
"Node_id": "10023"
},
{
"Node_id": "10056"
},
{
"Node_id": "00000"
}
],
"utc": "136199375611"
}
EDIT
just edit the POJO class to match as per the JSON string
class NodesDetail{
private ArrayList<NoteId> Nodes;
private String utc;
private String DeliveryTime;
private String Ifr;
}
class NoteId{
private String Node_id;
private String count;
}
...
NodesDetail obj = new Gson().fromJson(reader, NodesDetail.class);
System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(obj));
output:
{
"Nodes": [
{
"Node_id": "10023",
"count": "1"
},
{
"Node_id": "10056",
"count": "2"
},
{
"Node_id": "+00000",
"count": "1"
},
{
"Node_id": "-00000",
"count": "6"
}
],
"utc": "136199375611",
"DeliveryTime": "Tue 23rd jun 2014 12:45 AM",
"Ifr": "2333"
}