XMLSerializer missing tags - java

I have a big problem with XMLSerializer... I use JSON lib (net.sf.json) and this is my code:
public static void main(String[] args) throws Exception {
String xmlInputMsg = "<state>\n" +
" <appId>newID</appId>\n" +
" <newDocuments>\n" +
" <nameValues>\n" +
" <name>test1</name>\n" +
" <value>123456</value>\n" +
" </nameValues>\n" +
" <nameValues>\n" +
" <name>test2</name>\n" +
" <value>987654</value>\n" +
" </nameValues>\n" +
" </newDocuments>\n" +
" <oldDocument>\n" +
" <oldAppId>true</oldAppId>\n" +
" <name>App</name>\n" +
" <source>NA</source>\n" +
" </oldDocument>\n" +
"</state>";
String result = null;
XMLSerializer xmlSerializer = new XMLSerializer();
xmlSerializer.setForceTopLevelObject(true);
result = XMLSerializer.class.toGenericString();
System.out.println(result);
}
My current output is:
"state": {
"appId": "newID",
"newDocuments": [
{
"name": "test1",
"value": "123456"
},
{
"name": "test2",
"value": "987654"
}
],
"oldDocument": {
"oldAppId": "true",
"name": "App",
"source": "NA"
}
}}
But I would like have this msg:
{
"state": {
"appId": "newID",
"newDocuments": {
"nameValues": [
{
"name": "test1",
"value": "123456"
},
{
"name": "test2",
"value": "987654"
}
]
},
"oldDocument": {
"oldAppId": "true",
"name": "App",
"source": "NA"
}
}}
Unfortunately now I missing tag nameValues. Could someone help me with this?

Related

Apply some condition on jsonnode and filter resultset in Java

I have an array of Jsonnodes. something like that
[
{
"coupon": "VAR",
"currency": "USD",
"sip": "94989WAX5",
"lastModifiedDate": "2022-09-23T08:16:25Z"
},
{
"coupon": "VAR1",
"currency": "USD",
"sip": "94989WAX5",
"lastModifiedDate": "2022-09-21T08:16:25Z"
},
{
"coupon": "VAR3",
"currency": "USD",
"sip": "XHBRYWEB1",
"lastModifiedDate": "2022-09-20T08:16:25Z"
}
]
I have a requirement, if the sip value of two nodes are same then I need to pick only that sip which lastModifiedDate is latest. In above example the final output should be remaining two nodes.
[
{
"coupon": "VAR",
"currency": "USD",
"sip": "94989WAX5",
"lastModifiedDate": "2022-09-23T08:16:25Z"
},
{
"coupon": "VAR3",
"currency": "USD",
"sip": "XHBRYWEB1",
"lastModifiedDate": "2022-09-20T08:16:25Z"
}
]
I was try to solve it by creating HashMap<String,JsonNode> where Sip is the key and the JsonNode is complete node. It doesn't seems to be a cleaner way. is there any other way to achieve it. I am using fasterxml.jackson.databind.JsonNode
Map<String, JsonNode> map =
jsonNodeList.stream()
.collect(
toMap(
jsonNode -> jsonNode.get("sip").asText(),
jsonNode -> jsonNode,
(jsonNode1, jsonNode2) -> {
boolean after =
LocalDateTime.parse(String.valueOf(jsonNode1.get("lastModifiedDate")))
.isAfter(
LocalDateTime.parse(
String.valueOf(jsonNode2.get("lastModifiedDate"))));
return after ? jsonNode1 : jsonNode2;
},
HashMap::new));
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"[" +
" {" +
" \"coupon\": \"VAR\"," +
" \"currency\": \"USD\"," +
" \"sip\": \"94989WAX5\"," +
" \"lastModifiedDate\": \"2022-09-23T08:16:25Z\"" +
" }," +
" {" +
" \"coupon\": \"VAR1\"," +
" \"currency\": \"USD\"," +
" \"sip\": \"94989WAX5\"," +
" \"lastModifiedDate\": \"2022-09-21T08:16:25Z\"" +
" }," +
" {" +
" \"coupon\": \"VAR3\"," +
" \"currency\": \"USD\"," +
" \"sip\": \"XHBRYWEB1\"," +
" \"lastModifiedDate\": \"2022-09-20T08:16:25Z\"" +
" }" +
" ]");
Transformation
JsonNode node = josson.getNode("group(sip)#.elements.findByMax(lastModifiedDate)");
System.out.println(node.toPrettyString());
Output
[ {
"coupon" : "VAR",
"currency" : "USD",
"sip" : "94989WAX5",
"lastModifiedDate" : "2022-09-23T08:16:25Z"
}, {
"coupon" : "VAR3",
"currency" : "USD",
"sip" : "XHBRYWEB1",
"lastModifiedDate" : "2022-09-20T08:16:25Z"
} ]

Jolt transform JSON array keep rest of the fields

How to keep other fields in the Jolt transform JSON array, I am trying to use wildcard but fields are not added in the final output?
Here is the example input I am using
[
{
"foundduring": "D-DC",
"user_type": "type1",
"location": "location1"
},
{
"foundduring": "D-DG",
"user_type": "type2",
"location": "location2"
},
{
"foundduring": "D-DI",
"user_type": "type3",
"location": "location3"
}
]
I am using the following Jolt transformation and also trying wildcard:
[
{
"operation": "shift",
"spec": {
"*": {
"foundduring": {
"D-DC": {
"#CycleCount": "[&3].foundduring"
},
"D-DG": {
"#Pick": "[&3].foundduring"
},
"D-DI": {
"#Issue": "[&3].foundduring"
}
},
"#": "&"
}
}
}
]
Following is my expected output where shift operation happened and then need to keep all other fields as it it
[
{
"foundduring" : "CycleCount",
"user_type" : "type1",
"location" : "location1"
},
{
"foundduring" : "Pick",
"user_type" : "type2",
"location" : "location2"
},
{
"foundduring" : "Issue",
"user_type" : "type3",
"location" : "location3"
}
]
Actual Output coming:
[
{
"foundduring": "CycleCount"
},
{
"foundduring": "Pick"
},
{
"foundduring": "Issue"
}
]
Consider using "*" wildcard as else case instead of "#" such as
[
{
"operation": "shift",
"spec": {
"*": {
"foundduring": {
"D-DC": {
"#CycleCount": "[&3].&2"
},
"D-DG": {
"#Pick": "[&3].&2"
},
"D-DI": {
"#Issue": "[&3].&2"
}
},
"*": "[&1].&"
}
}
}
]
Btw, no need to get the key name "foundduring", just use &2 substitution to go 2 level up from the current branch and grab that value.
The demo on the site http://jolt-demo.appspot.com/ is
You may consider another library Josson.
https://github.com/octomix/josson
Deserialization
Josson josson = Josson.fromJsonString(
"[" +
" {" +
" \"foundduring\": \"D-DC\"," +
" \"user_type\": \"type1\"," +
" \"location\": \"location1\"" +
" }," +
" {" +
" \"foundduring\": \"D-DG\"," +
" \"user_type\": \"type2\"," +
" \"location\": \"location2\"" +
" }," +
" {" +
" \"foundduring\": \"D-DI\"," +
" \"user_type\": \"type3\"," +
" \"location\": \"location3\"" +
" }" +
"]");
Transformation
JsonNode node = josson.getNode(
"field(foundduring.caseValue('D-DC','CycleCount','D-DG','Pick','D-DI','Issue'))");
System.out.println(node.toPrettyString());
Output
[ {
"foundduring" : "CycleCount",
"user_type" : "type1",
"location" : "location1"
}, {
"foundduring" : "Pick",
"user_type" : "type2",
"location" : "location2"
}, {
"foundduring" : "Issue",
"user_type" : "type3",
"location" : "location3"
} ]

Failed to Parse JSON [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I am getting failed to parse JSON on my JS code, and when I tried to print it on console and beautify it on online JSON beautifier. It gives me this error
Parse error on line 1:
...-06-05 11:21:48.0"} ","productSellCatego
-----------------------^
Expecting 'EOF', '}', ',', ']', got 'STRING'
JSON
[{"id":"1" ,"storeName":"MyStore" ,"uniqueStoreCode":"MytURz718" ,"storeDescription":"how about this store" ,"storeStatus":"0" ,"gstinNumber":"mFnbkYGU5BdmijB0ZSp9Xw==" ,"sellerPanNumber":"MNBVK8520U" ,"storeAccountNumber":"Lf4HJliBJzWXIcYUW2CD7Q==" ,"country":"India" ,"state":"Uttar" ,"city":"Ghaziabad" ,"address":"b-1/136 s-1" ,"storeAccountHolderName":"Ramesh Sharma" ,"bankAccountType":"0" ,"addedAt":"2021-08-12 09:40:37.0" ,"storeOwner":{"id":"2", "uniqueUserId":"sec7M3dkH", "firstName":"Ramesh", "lastName":"Sharma", "dateOfBirth":"1979-07-23", "email":"sharmaascent6#gmail.com", "userName":"seller", "status":"0", "role":"1", "country":"null", "state":"null", "city":"null", "zipCode":"null", "address":"null", "gender":"0", "phoneNumber":"+919811708085", "addedAt":"2021-06-05 11:21:48.0"} ","productSellCategory":{"productCatId":"6", "categoryName":"Clothing and Accessories"}" ,"addedAt":"2021-08-12 09:40:37.0" } , {"id":"2" ,"storeName":"MyStore" ,"uniqueStoreCode":"My1mQfYDF" ,"storeDescription":"how about this store" ,"storeStatus":"0" ,"gstinNumber":"h/GcxLRmRljFa0nPE7GtRg==" ,"sellerPanNumber":"MNBVK8520U" ,"storeAccountNumber":"PkEBwQB/eiLktZBe8tDGPw==" ,"country":"India" ,"state":"Uttar" ,"city":"Ghaziabad" ,"address":"b-1/136 s-1" ,"storeAccountHolderName":"Ramesh Sharma" ,"bankAccountType":"0" ,"addedAt":"2021-08-12 09:43:02.0" ,"storeOwner":{"id":"2", "uniqueUserId":"sec7M3dkH", "firstName":"Ramesh", "lastName":"Sharma", "dateOfBirth":"1979-07-23", "email":"sharmaascent6#gmail.com", "userName":"seller", "status":"0", "role":"1", "country":"null", "state":"null", "city":"null", "zipCode":"null", "address":"null", "gender":"0", "phoneNumber":"+919811708085", "addedAt":"2021-06-05 11:21:48.0"} ","productSellCategory":"{"productCatId":"6", "categoryName":"Clothing and Accessories"}","addedAt":"2021-08-12 09:43:02.0" } , {"id":"3" ,"storeName":"Sharma Ji Store" ,"uniqueStoreCode":"Shlv1BXrS" ,"storeDescription":"This is a beauty products store" ,"storeStatus":"0" ,"gstinNumber":"njauEy1GZ+qEkLt02av/Fw==" ,"sellerPanNumber":"MNBVK8520U" ,"storeAccountNumber":"oc6ZgAfuufHkOt0fyO/lNQ==" ,"country":"India" ,"state":"Delhi" ,"city":"East" ,"address":"gali no.16 adarsh mohhala" ,"storeAccountHolderName":"Ramesh Sharma" ,"bankAccountType":"0" ,"addedAt":"2021-08-12 09:45:58.0" ,"storeOwner":{"id":"2", "uniqueUserId":"sec7M3dkH", "firstName":"Ramesh", "lastName":"Sharma", "dateOfBirth":"1979-07-23", "email":"sharmaascent6#gmail.com", "userName":"seller", "status":"0", "role":"1", "country":"null", "state":"null", "city":"null", "zipCode":"null", "address":"null", "gender":"0", "phoneNumber":"+919811708085", "addedAt":"2021-06-05 11:21:48.0"} ","productSellCategory":{"productCatId":"3", "categoryName":"Beauty"} ","addedAt":"2021-08-12 09:45:58.0" } ]
My Functions to print strings are
storeOwner
#Override
public String toString() {
return "{\"id\":\"" + id + "\", \"uniqueUserId\":\"" + uniqueUserId + "\", \"firstName\":\"" + firstName
+ "\", \"lastName\":\"" + lastName + "\", \"dateOfBirth\":\"" + dateOfBirth + "\", \"email\":\"" + email
+ "\", \"userName\":\"" + userName + "\", \"status\":\"" + status + "\", \"role\":\"" + role
+ "\", \"country\":\"" + country + "\", \"state\":\"" + state + "\", \"city\":\"" + city
+ "\", \"zipCode\":\"" + zipCode + "\", \"address\":\"" + address + "\", \"gender\":\"" + gender
+ "\", \"phoneNumber\":\"" + phoneNumber + "\", \"addedAt\":\"" + addedAt + "\"}";
}
stores
#Override
public String toString() {
return "{\"" +(id != null ? "id\":\"" + id + "\" " : "")
+(storeName != null ? ",\"storeName\":\"" + storeName + "\" " : "")
+(uniqueStoreCode != null ? ",\"uniqueStoreCode\":\"" + uniqueStoreCode + "\" " : "")
+(storeDescription != null ? ",\"storeDescription\":\"" + storeDescription + "\" " : "")
+(storeStatus != null ? ",\"storeStatus\":\"" + storeStatus + "\" " : "")
+(gstinNumber != null ? ",\"gstinNumber\":\"" + gstinNumber + "\" " : "")
+(sellerPanNumber != null ? ",\"sellerPanNumber\":\"" + sellerPanNumber + "\" " : "")
+(storeAccountNumber != null ? ",\"storeAccountNumber\":\"" + storeAccountNumber + "\" " : "")
+(country != null ? ",\"country\":\"" + country + "\" " : "")
+(state != null ? ",\"state\":\"" + state + "\" " : "")
+(city != null ? ",\"city\":\"" + city + "\" " : "")
+(address != null ? ",\"address\":\"" + address + "\" " : "")
+(storeAccountHolderName != null ? ",\"storeAccountHolderName\":\"" + storeAccountHolderName + "\" " : "")
+(bankAccountType != null ? ",\"bankAccountType\":\"" + bankAccountType + "\" " : "")
+(addedAt != null ? ",\"addedAt\":\"" + addedAt + "\" " : "")
+ (Hibernate.isInitialized(storeOwner) && storeOwner != null ? ",\"storeOwner\":" + storeOwner + " \"" : "")
+ (Hibernate.isInitialized(productSellCategory) && productSellCategory != null ? ",\"productSellCategory\":" + productSellCategory + " \"" : "")
// (Hibernate.isInitialized(deliveryStatusSet) && deliveryStatusSet != null ? ", \"deliveryStatusSet\":" + deliveryStatusSet : "")
+(addedAt != null ? ",\"addedAt\":\"" + addedAt + "\" " : "")
+ "}\t";
}
Product category
#Override
public String toString() {
return "{\"productCatId\":\"" + productCatId + "\", \"categoryName\":\"" + categoryName + "\"}";
// return "{\"" +(productCatId != null ? "productCatId\":\"" + productCatId + "\" " : "")
// +(categoryName != null ? ",\"categoryName\":\"" + categoryName + "\" " : "")
// + "}\t";
}
In your json object is placed with quotes, removing those quotes should make it valid json
e.g
change ","productSellCategory":"{ to , "productSellCategory": {
your json after removing quotes is
[
{
"id": "1",
"storeName": "MyStore",
"uniqueStoreCode": "MytURz718",
"storeDescription": "how about this store",
"storeStatus": "0",
"gstinNumber": "mFnbkYGU5BdmijB0ZSp9Xw==",
"sellerPanNumber": "MNBVK8520U",
"storeAccountNumber": "Lf4HJliBJzWXIcYUW2CD7Q==",
"country": "India",
"state": "Uttar",
"city": "Ghaziabad",
"address": "b-1/136 s-1",
"storeAccountHolderName": "Ramesh Sharma",
"bankAccountType": "0",
"addedAt": "2021-08-12 09:40:37.0",
"storeOwner": {
"id": "2",
"uniqueUserId": "sec7M3dkH",
"firstName": "Ramesh",
"lastName": "Sharma",
"dateOfBirth": "1979-07-23",
"email": "sharmaascent6#gmail.com",
"userName": "seller",
"status": "0",
"role": "1",
"country": "null",
"state": "null",
"city": "null",
"zipCode": "null",
"address": "null",
"gender": "0",
"phoneNumber": "+919811708085",
"addedAt": "2021-06-05 11:21:48.0"
},
"productSellCategory": {
"productCatId": "6",
"categoryName": "Clothing and Accessories"
},
"addedAt": "2021-08-12 09:40:37.0"
},
{
"id": "2",
"storeName": "MyStore",
"uniqueStoreCode": "My1mQfYDF",
"storeDescription": "how about this store",
"storeStatus": "0",
"gstinNumber": "h/GcxLRmRljFa0nPE7GtRg==",
"sellerPanNumber": "MNBVK8520U",
"storeAccountNumber": "PkEBwQB/eiLktZBe8tDGPw==",
"country": "India",
"state": "Uttar",
"city": "Ghaziabad",
"address": "b-1/136 s-1",
"storeAccountHolderName": "Ramesh Sharma",
"bankAccountType": "0",
"addedAt": "2021-08-12 09:43:02.0",
"storeOwner": {
"id": "2",
"uniqueUserId": "sec7M3dkH",
"firstName": "Ramesh",
"lastName": "Sharma",
"dateOfBirth": "1979-07-23",
"email": "sharmaascent6#gmail.com",
"userName": "seller",
"status": "0",
"role": "1",
"country": "null",
"state": "null",
"city": "null",
"zipCode": "null",
"address": "null",
"gender": "0",
"phoneNumber": "+919811708085",
"addedAt": "2021-06-05 11:21:48.0"
},
"productSellCategory": {
"productCatId": "6",
"categoryName": "Clothing and Accessories"
},
"addedAt": "2021-08-12 09: 43: 02.0"
},
{
"id": "3",
"storeName": "Sharma Ji Store",
"uniqueStoreCode": "Shlv1BXrS",
"storeDescription": "This is a beauty products store",
"storeStatus": "0",
"gstinNumber": "njauEy1GZ+qEkLt02av/Fw==",
"sellerPanNumber": "MNBVK8520U",
"storeAccountNumber": "oc6ZgAfuufHkOt0fyO/lNQ==",
"country": "India",
"state": "Delhi",
"city": "East",
"address": "gali no.16 adarsh mohhala",
"storeAccountHolderName": "Ramesh Sharma",
"bankAccountType": "0",
"addedAt": "2021-08-12 09: 45: 58.0",
"storeOwner": {
"id": "2",
"uniqueUserId": "sec7M3dkH",
"firstName": "Ramesh",
"lastName": "Sharma",
"dateOfBirth": "1979-07-23",
"email": "sharmaascent6#gmail.com",
"userName": "seller",
"status": "0",
"role": "1",
"country": "null",
"state": "null",
"city": "null",
"zipCode": "null",
"address": "null",
"gender": "0",
"phoneNumber": "+919811708085",
"addedAt": "2021-06-05 11: 21: 48.0"
},
"productSellCategory": {
"productCatId": "3",
"categoryName": "Beauty"
},
"addedAt": "2021-08-12 09: 45: 58.0"
}
]
This is a very error prone way to create JSON.
Instead, create a POJO and use Gson or Jackson to convert it to a JSON string:
import com.google.gson.Gson;
public class SO69240970 {
static class MyData {
String a;
String b;
int c;
public MyData(String a, String b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
}
public static void main(String[] args) {
MyData m = new MyData("A value", "B \"value\"", 102);
System.out.println(new Gson().toJson(m));
}
}
Output: {"a":"A value","b":"B \"value\"","c":102}
Maven dependency:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.8</version>
</dependency>
this is what worked for me
instead of
(Hibernate.isInitialized(storeOwner) && storeOwner != null ? ","storeOwner":" + storeOwner + " "" : "")
+ (Hibernate.isInitialized(productSellCategory) && productSellCategory != null ? ","productSellCategory":" + productSellCategory + " "" : "")
I used
(Hibernate.isInitialized(storeOwner) && storeOwner != null ? ","storeOwner":" + storeOwner :"" )
+ (Hibernate.isInitialized(productSellCategory) && productSellCategory != null ? ","productSellCategory":" + productSellCategory : "")

How to get level of each object in json where objects can have any number of children of same object type

I have an Employee level structure like below image
This structure is populated and stored with help of json like this
{
"name": "Lao Lao",
"title": "general manager",
"children": [
{
"name": "Bo Miao",
"title": "department manager",
"children": [
{
"name": "Li Jing",
"title": "senior engineer"
},
{
"name": "Li Xin",
"title": "senior engineer",
"children": [
{
"name": "To To",
"title": "engineer"
},
{
"name": "Fei Fei",
"title": "engineer"
},
{
"name": "Xuan Xuan",
"title": "engineer"
}
]
}
]
},
{
"name": "Su Miao",
"title": "department manager",
"children": [
{
"name": "Pang Pang",
"title": "senior engineer"
},
{
"name": "Hei Hei",
"title": "senior engineer",
"children": [
{
"name": "Xiang Xiang",
"title": "UE engineer"
},
{
"name": "Dan Dan",
"title": "engineer"
},
{
"name": "Zai Zai",
"title": "engineer"
}
]
}
]
}
]
}
I want to parse this Json to get all the objects with the level and parent name like this
{name = Lao lao , parent = null , level = 1 }
{name = Bao Miao , parent = Lao lao , level = 2 }
..................................................
{name = Li Jing , parent = Bao Miao , level = 3 }
How can we parse this with help of java?If there is any library with such functionality, please let me know.
Implement Model definition as below. Also put level and parentName in Model class.
class Employee{
String name;
String title;
Employee children[];
int level;
String parentName;
#Override
public String toString(){
return "{name = "+name+" , parent = "+parentName+ ", level = "+level+ " }";
}
}
Parse the json data using GSON API.
Employee e= new Gson().fromJson(new JsonReader(new FileReader("file.json")), Employee.class);
This is complete program for you. Finally i was able to write all the code for you after spending an hour on this. Working fine so far :)
import java.io.FileReader;
import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
public class ParseJson {
public static void main(String a[]) {
Gson g = new Gson();
try {
Employee e = g.fromJson(new JsonReader(new FileReader("file.json")), Employee.class);
parseEmployees(e);
printEmployee(e);
} catch (Exception e1) {
e1.printStackTrace();
}
}
private static void parseEmployees(Employee e) {
setParentAndLevel(e, 1, null);
}
private static void setParentAndLevel(Employee e, int lvl, String parent) {
e.level = lvl;
e.parentName = parent;
if (e.children != null && e.children.length > 0) {
lvl++;
for (Employee emp : e.children) {
setParentAndLevel(emp, lvl, e.name);
}
}
}
public static void printEmployee(Employee e){
System.out.println(e);
if (e.children != null && e.children.length > 0) {
for (Employee emp : e.children) {
printEmployee(emp);
}
}else{
return ;
}
}
}
class Employee {
String name;
String title;
Employee children[];
int level;
String parentName;
#Override
public String toString() {
return "{name = " + name + " , parent = " + parentName + ", level = " + level + " }";
}
}
Output :
{name = Lao Lao , parent = null, level = 1 }
{name = Bo Miao , parent = Lao Lao, level = 2 }
{name = Li Jing , parent = Bo Miao, level = 3 }
{name = Li Xin , parent = Bo Miao, level = 3 }
{name = To To , parent = Li Xin, level = 4 }
{name = Fei Fei , parent = Li Xin, level = 4 }
{name = Xuan Xuan , parent = Li Xin, level = 4 }
{name = Su Miao , parent = Lao Lao, level = 2 }
{name = Pang Pang , parent = Su Miao, level = 3 }
{name = Hei Hei , parent = Su Miao, level = 3 }
{name = Xiang Xiang , parent = Hei Hei, level = 4 }
{name = Dan Dan , parent = Hei Hei, level = 4 }
{name = Zai Zai , parent = Hei Hei, level = 4 }
look like I have so much time and waste my time doing this for you because it sound challenging basically I turn this into a jsonObject first then I do a bfs graph walk to find the level and parent told you it is a graph problem again there is some bug on the level but I want the community or you to fix the bug yourself
EDIT: I have fix the level bug for you already again ask me if you have any question
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception{
String JSON_STRING = "{\n" +
" \"name\": \"Lao Lao\",\n" +
" \"title\": \"general manager\",\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"Bao Miao\",\n" +
" \"title\": \"department manager\",\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"Li Jing\",\n" +
" \"title\": \"senior engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Li Xin\",\n" +
" \"title\": \"senior engineer\",\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"To To\",\n" +
" \"title\": \"engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Fei Fei\",\n" +
" \"title\": \"engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Xuan Xuan\",\n" +
" \"title\": \"engineer\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" },\n" +
" {\n" +
" \"name\": \"Su Miao\",\n" +
" \"title\": \"department manager\",\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"Pang Pang\",\n" +
" \"title\": \"senior engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Hei Hei\",\n" +
" \"title\": \"senior engineer\",\n" +
" \"children\": [\n" +
" {\n" +
" \"name\": \"Xiang Xiang\",\n" +
" \"title\": \"UE engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Dan Dan\",\n" +
" \"title\": \"engineer\"\n" +
" },\n" +
" {\n" +
" \"name\": \"Zai Zai\",\n" +
" \"title\": \"engineer\"\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
" }\n" +
" ]\n" +
"}] \n";
JSONObject obj = new JSONObject(JSON_STRING);
Deque<JSONObject> deque = new ArrayDeque<>();
Map<String, String> res = new HashMap<>();
int level = 1;
res.put("NULL", obj.getString("name")+ "-" + level);
deque.add(obj);
Map<String, Integer> levelmap = new HashMap<>();
levelmap.put(obj.getString("name"), 1);
while (!deque.isEmpty()){
JSONObject u = deque.poll();
try {
JSONArray children = u.getJSONArray("children");
for (int i = 0; i < children.length(); i++) {
deque.add(children.getJSONObject(i));
levelmap.put(children.getJSONObject(i).getString("name"), levelmap.get(u.getString("name")) + 1);
res.put(children.getJSONObject(i).getString("name"), u.getString("name") + "-" + levelmap.get(children.getJSONObject(i).getString("name")));
}
}catch (JSONException jex){
System.out.println("end of the tree");
}
}
//turn it back into a json array format
String str = new String("[]");
JSONArray jsonArray = new JSONArray(str);
System.out.println(res);
for(String key: res.keySet()){
String st = new String("{}");
JSONObject jsonObject = new JSONObject(st);
//key is parent
String[] tok = res.get(key).split("-");
String child = tok[0];
String mylevel = tok[1];
jsonObject.put("name", key);
jsonObject.put("level", mylevel);
jsonObject.put("parent", child);
jsonArray.put(jsonObject);
}
System.out.println(jsonArray.toString(2));
}
}
output:
[
{
"parent": "Hei Hei",
"level": "4",
"name": "Xiang Xiang"
},
{
"parent": "Lao Lao",
"level": "2",
"name": "Bao Miao"
},
{
"parent": "Lao Lao",
"level": "1",
"name": "NULL"
},
{
"parent": "Su Miao",
"level": "3",
"name": "Hei Hei"
},
{
"parent": "Hei Hei",
"level": "4",
"name": "Dan Dan"
},
{
"parent": "Hei Hei",
"level": "4",
"name": "Zai Zai"
},
{
"parent": "Li Xin",
"level": "4",
"name": "Xuan Xuan"
},
{
"parent": "Su Miao",
"level": "3",
"name": "Pang Pang"
},
{
"parent": "Li Xin",
"level": "4",
"name": "Fei Fei"
},
{
"parent": "Li Xin",
"level": "4",
"name": "To To"
},
{
"parent": "Bao Miao",
"level": "3",
"name": "Li Jing"
},
{
"parent": "Lao Lao",
"level": "2",
"name": "Su Miao"
},
{
"parent": "Bao Miao",
"level": "3",
"name": "Li Xin"
}
]
Yes, you can do this. First, you need to map this json to POJO.
So, Create a model class like this
class Employee{
String name;
String title;
List<Employee> Children;
}
If you can map this json to POJO. Then you only need to run through a loop to get what you want.

how to use maxAggregation in jest elasticsearch

i want to get the maximun id form a subfield of an aptitude object,
{
"mappings": {
"aptitude": {
"dynamic": "strict",
"properties": {
"id": {
"type": "long"
},
"es": {
"type": "text"
},
"en": {
"type": "text"
},
"behaviors": {
"properties": {
"id": {
"type": "long"
},
"es": {
"type": "text"
},
"en": {
"type": "text"
}
}
}
}
}
}
as you can see the aptitude have an array of behaviors who in turn have an id, afaik i should use the maxAggregation from Jest, but cant find a decent example of how to do it in java, can someone help?
i found the way like this:
String query = "{\n"
+" \"query\" : {\n"
+" \"match\" : {\"id\":" + aptitudeId + "}\n"
+" },\n"
+" \"aggs\" : {\n"
+" \"max1\" : {\n"
+" \"max\" : {\n"
+" \"field\" : \"behaviors.id\"\n"
+" }\n"
+" }\n"
+" }\n"
+"}";
i was looking into the aggregation builders from jest but doing it via query was much easier.
the return looks like this:
Search search = new Search.Builder(query)
.addIndex(aptitudeIndexName)
.addType(aptitudeTypeName)
.build();
try {
SearchResult result = client.execute(search);
MaxAggregation max1 = result.getAggregations().getMaxAggregation("max1");
Double max = max1.getMax();
return max.longValue() + 1;//so it would add 1 to the current maximum id

Categories

Resources