How to count by attribute in JSON? - java

I have the following JSON:
{
"items": [
{
"id": "1",
"name": "John",
"location": {
"town": {
"id": "10"
},
"address": "600 Fake Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
},
{
"id": "2",
"name": "Sarah",
"location": {
"town": {
"id": "10"
},
"address": "76 Evergreen Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
},
{
"id": "3",
"name": "Hamed",
"location": {
"town": {
"id": "20"
},
"address": "50 East A Street",
},
"creation_date": "2010-01-19",
"last_modified_date": "2017-05-18"
}
]
}
And I need to get something like this, count how many times each townId appears:
[ { "10": 2 }, {"20": 1 }]
I'm trying to find the most eficient way to do this. Any idea?

Most efficient way is to load the String in a StringBuilder and remove all line breaks and white spaces. Then search for index of "town":{"id":" string (town start index) and then search for the end index (String `"}'). Using the 2 indexes you can extract town ids and count them.
No need to deserialize the JSON into POJO objects:) and extract values by xpath from the POJOs.

Related

How to get json key values by another key value

I have a JSON output like this:
{
"items": [
{
"id": "1",
"name": "Anna",
"values": [
{
"code": "Latin",
"grade": 1
},
{
"code": "Maths",
"grade": 5
}
]
},
{
"id": "2",
"name": "Mark",
"values": [
{
"code": "Latin",
"grade": 5
},
{
"code": "Maths",
"grade": 5
}
]
}
]
}
I need to get field values for "name": "Anna". I am getting RestAssured Response and would like to use my beans to do that, but I can also use jsonPath() or jsonObject(), but I don't know how. I searched many topics but did not find anything.

How to parse a flattened Json?

I have a requirement to use flattened structure JSON, for example below hierarchical Json:
{
"employees": [
{
"employee1": {
"employeeId": 123,
"name": "ABC",
"type": "permanent",
"address": {
"street": "",
"city": "",
"zipcode": 123456
},
"phoneNumbers": [
123456,
987654
],
"designation": "Manager",
"properties": {
"age": "29 years",
"joiningDate": "17-may-2017",
"salary": "1000 USD"
}
}
},
{
"employee2": {
"employeeId": 123,
"name": "XYZ",
"type": "parttime",
"address": {
"street": "",
"city": "",
"zipcode": 345645
},
"phoneNumbers": [
345332,
675444
],
"designation": "Contractor",
"properties": {
"age": "35 years",
"joiningatDate": "17-june-2015",
"salary": "700 USD"
}
}
}
]
}
**Could be represented as flat structure Json as below(generated this using json-flattener):**
{
"employees[0].employee1.address.zipcode": 123456,
"employees[0].employee1.address.city": "",
"employees[0].employee1.address.street": "",
"employees[0].employee1.name": "ABC",
"employees[0].employee1.employeeId": 123,
"employees[0].employee1.designation": "Manager",
"employees[0].employee1.type": "permanent",
"employees[0].employee1.phoneNumbers[0]": 123456,
"employees[0].employee1.phoneNumbers[1]": 987654,
"employees[0].employee1.properties.joiningDate": "17-may-2017",
"employees[0].employee1.properties.salary": "1000 USD",
"employees[0].employee1.properties.age": "29 years",
"employees[1].employee2.address.zipcode": 345645,
"employees[1].employee2.address.city": "",
"employees[1].employee2.address.street": "",
"employees[1].employee2.name": "XYZ",
"employees[1].employee2.employeeId": 123,
"employees[1].employee2.designation": "Contractor",
"employees[1].employee2.type": "parttime",
"employees[1].employee2.phoneNumbers[0]": 345332,
"employees[1].employee2.phoneNumbers[1]": 675444,
"employees[1].employee2.properties.joiningDate": "17-june-2015",
"employees[1].employee2.properties.salary": "700 USD",
"employees[1].employee2.properties.age": "35 years"
}
My problem is if my service receives flattened Json like above, how to convert it to java domain objects automatically like:
class Employee{
Address address;
Properties property;
}
Does any Java Json parser supports this automatic conversion or i will have to implement own parsing logic splitting the keys based on dot in the keys?
Thanks in Advance.
You can take a look at json-flattener.
It does what you want, and even more.
The usage is simple as this:
String json = "{ \"a\" : { \"b\" : 1, \"c\": null, \"d\": [false, true] }, \"e\": \"f\", \"g\":2.3 }";
String jsonStr = JsonFlattener.flatten(json);
System.out.println(jsonStr);
// Output: {"a.b":1,"a.c":null,"a.d[0]":false,"a.d[1]":true,"e":"f","g":2.3}

MongoDB query and select inner object

I want to query for inner object and select only filtered inner objects from mongoddb document.
Consider below mongodb document.
{
"schools": [
{
"name": "ABC",
"students": [
{
"name": "ABC 1",
"class": 1
},
{
"name": "ABC 2",
"class": 2
},
{
"name": "ABC 3",
"class": 1
}
]
},
{
"name": "XYZ",
"students": [
{
"name": "XYZ 1",
"class": 1
},
{
"name": "XYZ 2",
"class": 2
}
]
}
]
}
I want to select only students in class 1.
expected result json as below.
{
"school": {
"name": "ABC",
"students": [
{
"name": "ABC 1",
"class": 1
},
{
"name": "ABC 3",
"class": 1
}
]
},
"school": {
"name": "XYZ",
"students": [
{
"name": "XYZ 1",
"class": 1
}
]
}
}
Even below result is fine with me.
{
"students": [
{
"name": "ABC 1",
"class": 1
},
{
"name": "ABC 3",
"class": 1
},
{
"name": "XYZ 1",
"class": 1
}
]
}
Please help me to get this done.
Really helpful if can provide mongodb query.
I am using mongodb with spring data in my application.
You can search for mongo db array nested record search. The example code is here. Document is here.
db.your_collection_name.find({'school.student.class':1})
If you only want students do flatmap for your results. Here is document for flatmap in mongodb
Finally I could be able to find the query.
First I have to unwind and apply matching criteria. this was work for me.
db.{mycollection}.aggregate(
[
{ $unwind: '$schools.students'},
{ $match : { "schools.students.class" : 1 } },
{ $project : { "schools.name" : 1, 'schools.students' : 1 } }
]
);

How i do grouping and transform data in java language

I try to use group by in Collectors.groupingBy in java but i can not get data form that i want.
I want to transform this form data :
[
{
"id": 1,
"name": "A",
"company_id": "01",
"company_name": "Company A",
},
{
"id": 2,
"name": "B",
"company_id": "01",
"company_name": "Company A",
},{
"id": 3,
"name": "C",
"company_id": "02",
"company_name": "Company B",
}
]
to this data form:
[
{
"company_id": "01",
"company_name": "Company A",
"member":[
{
"id": 1,
"name": "A"
},
{
"id": 2,
"name": "B"
}
]
},{
"company_id": "02",
"company_name": "Company B",
"member":[
{
"id": 3,
"name": "C"
}
]
}
]
this form group by company_id.
The way that I would do it:
First I would create pojo objects for both formats, then I would assign each value corresponding ones then I would convert the second object into json as using gson.

Tuple and bags to Json

I had 4 files. I joined all the files using Pig and obtained the final output and grouped the data as required. Now that I have my input something like this.
({(9723,(N,N)),({({(11,G),(H,House),(1,1ST),(02/25/2015)}),({(10,L),(H,House),(16,EMPTY),(02/25/2015)})})})
which is my pig output.
I want to convert it into JSON.
My output should look like this.
{
"department": {
"department_id": "9723",
"department_group": {
"flag1": "N",
"flag2": "N"
},
"employee_detail1": {
"employee_type": {
"code": "11",
"name": "G"
},
"employee_level": {
"code": "H",
"name": "House"
},
"employee_dmg": {
"code": "1",
"name": "1st"
},
"DOJ": "02/25/2015"
},
"employee_detail2": {
"employee_type": {
"code": "10",
"name": "L"
},
"employee_level": {
"code": "H",
"name": "House"
},
"employee_dmg": {
"code": "0",
"name": "No"
},
"DOJ": "02/25/2015"
}
}
}
There are 2 bags(meaning 2 employee details).... grouped by emp_id and employee group(tuple with flag1 and flag2)....
Can someone suggest me the best way to convert this into JSON...
You can STORE your data with JsonStorage, it will handle nicely a bag.

Categories