How to Iterate through Json schema and values based on keys - java

I have this JSON schema. I want to read it line by line and populate all "name" key values to an ArrayList of String until it reaches a key called entity. When it reaches to an entity I want to read the entity value and populate it to a String.After that loop should go through the remains of the schema and read all the "name" and populate to a new ArrayList until it meets another entity and so on...
I've only included a part of the schema it's a valid schema.
{
"entity": "Data",
"xement": [
{
"code": "MA",
"entity": "MH",
"attr": [
{
"name": "Id",
"position": 1
},
{
"name": "mdc",
"position": 4
},
{
"name": "minr",
"position": 10
},
{
"name": "mx",
"position": 11
}
],
"maltr": [
{
"name": "sub",
"position": 13
}
]
},
{
"code": "war",
"entity": "subl",
"attr": [
{
"name": "se",
"position": 1
},
{
"name": "go",
"position": 2
},
{
"name": "re",
"position": 4
},
{
"name": "pr",
"position": 10
},
{
"name": "mxp",
"position": 11
},
{
"name": "rpl",
"position": 45
},
{
"name": "rtr",
"position": 47
},
{
"name": "net",
"position": 55
}
],
"groups": [
{
"entity": "ro",
"grattr": [
{
"name": "rmn",
"position": 5
},
{
"name": "aib",
"position": 6
},
{
"name": "nxr",
"position": 7
},
{
"name": "xer",
"position": 8
},
{
"name": "rog",
"position": 9
},
{
"name": "ccc",
"position": 16
}
]
}
]
}
]
}
What is the best way to do that?

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.

Parsing JSON file to Java Object

Underneath is a JSON file where it used an OCR from a pdf file table containing data and gets the JSON below. I want the contents of "DATE", "SUM", "MULTIPLE" from this JSON and convert to JAVA so then I can be able to store it to database. I've heard Jackson or JSON validator would do the trick, but in this JSON format, how can I approach this? For instance, you'll see "content": "DATE", how can I convert "DATE" into JAVA object, and then later store to a DataBase?
{"metadata": [],
"pages": [
{
"margins": {
"top": -1,
"left": -1,
"bottom": -1,
"right": -1
},
"box": {
"l": 0,
"t": 0,
"w": 738,
"h": 952
},
"rotation": {
"degrees": "0",
"origin": {
"x": "369",
"y": "476"
},
"translation": {
"x": "0",
"y": "0"
}
},
"pageNumber": 1,
"elements": [
{
"id": 519,
"type": "paragraph",
"properties": {
"order": 0
},
"metadata": [],
"box": {
"l": 102.15,
"t": 118.92,
"w": 107.97,
"h": 106.68
},
"content": [
{
"id": 8,
"type": "line",
"properties": {
"order": 0,
"cr": 210.12,
"cl": 76.43
},
"metadata": [],
"box": {
"l": 168.78,
"t": 118.92,
"w": 41.35,
"h": 12.61
},
"content": [
{
"id": 7,
"type": "word",
"properties": {},
"metadata": [],
"box": {
"l": 168.78,
"t": 118.92,
"w": 41.35,
"h": 12.61
},
"content": "**DATE:**",
"font": 1
}
]
},
{
"id": 18,
"type": "line",
"properties": {
"order": 1,
"cr": 210.12,
"cl": 76.43
},
"metadata": [],
"box": {
"l": 102.15,
"t": 150.24,
"w": 104.5,
"h": 12.54
},
"content": [
{
"id": 16,
"type": "word",
"properties": {},
"metadata": [],
"box": {
"l": 102.15,
"t": 150.24,
"w": 46.49,
"h": 12.28
},
"content": "**SALES**",
"font": 1
},
{
"id": 17,
"type": "word",
"properties": {},
"metadata": [],
"box": {
"l": 152.34,
"t": 150.32,
"w": 54.31,
"h": 12.46
},
"content": "**ORDER:**",
"font": 1
}
]
}
There are many JSON parsers for Java:
Google GSON
Gson gson=new Gson();
MyObject=gson.fromJson(jsonString, MyObject.class);
javax.json
JsonReader jsonReader = Json.createReader(new StringReader(jsonString));
JsonObject jsonObject = jsonReader.readObject();
int myInt = jsonObject.getInt("myInt"); // or getString(), getJsonObject() etc.
JSON-Simple (similar usage to javax.json)
org.json: see this StackOverflow answer
and many others to choose from...

Java looping over object

I am new to Java - and I am trying to loop through an JSONArray to create a "label", "value" array.
JSONArray records = (JSONArray) sch.get("schData");
looks like this A)
{"emotional distress":4,"peer difficulties":6,"behavioural difficulties":8,"kind and helpful behaviour":8,"overall stress":32,"_sdqID":11,"hyperactivity and concentration":6}
and I want to loop through this object to create the following structure
B)
"chart": [{
"label": "Overall Stress",
"value": 89
},{
"label": "Emotional Stress",
"value": 1
},{
"label": "Behavioural difficulties",
"value": 29
},{
"label": "hyperactivity and concetration",
"value": 89
},{
"label": "Getting along with others",
"value": 19
},{
"label": "Keen and helpful behaviour",
"value": 99
}]
--
so as I create a record I want to capitilize the key - and not include the _sdqID element. How do I do it?
I could try and create something manually.
JSONObject row = new JSONObject();
row.put("label", "Emotional Distress");
row.put("value", ((JSONObject) records.get(i)).get("emotional distress"));
rowArray.add(row);
and I tried to put this inside a 2nd loop - but I start to get cast issues inside this. So I am not sure what's the best approach at this step.
for (int j = 0; j < ((JSONObject) records.get(i)).size(); j++) {
//code
}
You can use this code with some modification. I have changed the keys in output to Upper case you can write some according to you requirement.
public static void main(String[] args) {
String data = "{\"emotional distress\":4,\"peer difficulties\":6,\"behavioural difficulties\":8,\"kind and helpful behaviour\":8,\"overall stress\":32,\"_sdqID\":11,\"hyperactivity and concentration\":6}";
JSONObject json = new JSONObject();
JSONArray chart = new JSONArray();
json.put("chart", chart);
JSONObject jsonObject = new JSONObject(data);
Iterator<String> iterator = jsonObject.keys();
while(iterator.hasNext()) {
String key = iterator.next();
if(key.equalsIgnoreCase("_sdqID")) {
continue;
}
int value = jsonObject.getInt(key);
key = key.toUpperCase();
JSONObject row = new JSONObject();
row.put("label", key);
row.put("value", value);
chart.put(row);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
JSONObject obj = new JSONObject();
obj.put("emotional distress", 4);
obj.put("peer difficulties", 6);
obj.put("behavioural difficulties", 9);
JSONArray array = new JSONArray();
for(String key : obj.keySet()) {
JSONObject newObj = new JSONObject();
newObj.put("label", key);
newObj.put("value", obj.get(key));
array.put(newObj);
}
System.out.println(array);
}
Your A object is obj here and B is array.
You can get the JSON array directly, and then loop through it.
import org.json.*;
public class JsonIO {
public static void parseJson(StringBuffer sb){
// sb is the JSON string
JSONObject obj = new JSONObject(sb);
JSONArray arr = obj.getJSONArray("chart");
for (int i = 0; i< arr.length(); i++){
// loop through
System.out.println(arr.getJSONObject(i).getString("label")); // i.e
System.out.println(arr.getJSONObject(i).getString("value"));
}
}
}
Secondly, GSON lib. can be used. Here you can download.
public static void parseJson(String sb){
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject)jsonParser.parse(sb);
JsonArray jArray = jo.getAsJsonArray("chart"); // get json array
Gson gJson = new Gson();
ArrayList jsonObjArrayList = gJson.fromJson(jArray, ArrayList.class);
for (int i = 0; i< jsonObjArrayList.size(); i++){
System.out.println(jsonObjArrayList.get(i).toString());
}
}
Alankar you fixed it man, cheers. obj.keySet().iterator();" that will solve the problem. That's what I had to do to loop over the inner data.
ok so the variable sch looks like this
{"schData":[{"emotional distress":4,"peer difficulties":6,"behavioural difficulties":8,"kind and helpful behaviour":8,"overall stress":32,"_sdqID":11,"hyperactivity and concentration":6},{"emotional distress":4,"peer difficulties":8,"behavioural difficulties":6,"kind and helpful behaviour":5,"overall stress":28,"_sdqID":10,"hyperactivity and concentration":5},{"emotional distress":4,"peer difficulties":8,"behavioural difficulties":6,"kind and helpful behaviour":5,"overall stress":28,"_sdqID":9,"hyperactivity and concentration":5},{"emotional distress":2,"peer difficulties":2,"behavioural difficulties":4,"kind and helpful behaviour":2,"overall stress":13,"_sdqID":8,"hyperactivity and concentration":3},{"emotional distress":5,"peer difficulties":6,"behavioural difficulties":8,"kind and helpful behaviour":7,"overall stress":32,"_sdqID":7,"hyperactivity and concentration":6},{"emotional distress":7,"peer difficulties":6,"behavioural difficulties":8,"kind and helpful behaviour":9,"overall stress":34,"_sdqID":6,"hyperactivity and concentration":4},{"emotional distress":5,"peer difficulties":4,"behavioural difficulties":4,"kind and helpful behaviour":6,"overall stress":21,"_sdqID":5,"hyperactivity and concentration":2},{"emotional distress":1,"peer difficulties":0,"behavioural difficulties":0,"kind and helpful behaviour":0,"overall stress":1,"_sdqID":4,"hyperactivity and concentration":0}]}
here is the java code
JSONArray data = new JSONArray();
//System.out.println("sch" + sch);
JSONArray records = (JSONArray) sch.get("schData");
for (int i = 0; i < records.size(); i++) {
//code
JSONObject chart = new JSONObject();
JSONObject obj = (JSONObject) records.get(i);
JSONArray rowArray = new JSONArray();
for (Object key : obj.keySet()) {
String keyStr = (String)key;
Object keyvalue = obj.get(keyStr);
//Print key and value
// System.out.println("key: "+ keyStr + " value: " + keyvalue);
JSONObject row = new JSONObject();
row.put("label", keyStr);
row.put("value", keyvalue);
rowArray.add(row);
}
chart.put("chart", rowArray);
JSONObject chartRecord = new JSONObject();
chartRecord.put("title", "xxx");
chartRecord.put("contents", chart);
data.add(chartRecord);
}
//System.out.println("chart data: "+ data);
--- response is this
[{
"contents": {
"chart": [{
"label": "emotional distress",
"value": 4
}, {
"label": "peer difficulties",
"value": 6
}, {
"label": "behavioural difficulties",
"value": 8
}, {
"label": "kind and helpful behaviour",
"value": 8
}, {
"label": "overall stress",
"value": 32
}, {
"label": "_sdqID",
"value": 11
}, {
"label": "hyperactivity and concentration",
"value": 6
}]
},
"title": "xxx"
}, {
"contents": {
"chart": [{
"label": "emotional distress",
"value": 4
}, {
"label": "peer difficulties",
"value": 8
}, {
"label": "behavioural difficulties",
"value": 6
}, {
"label": "kind and helpful behaviour",
"value": 5
}, {
"label": "overall stress",
"value": 28
}, {
"label": "_sdqID",
"value": 10
}, {
"label": "hyperactivity and concentration",
"value": 5
}]
},
"title": "xxx"
}, {
"contents": {
"chart": [{
"label": "emotional distress",
"value": 4
}, {
"label": "peer difficulties",
"value": 8
}, {
"label": "behavioural difficulties",
"value": 6
}, {
"label": "kind and helpful behaviour",
"value": 5
}, {
"label": "overall stress",
"value": 28
}, {
"label": "_sdqID",
"value": 9
}, {
"label": "hyperactivity and concentration",
"value": 5
}]
},
"title": "xxx"
}, {
"contents": {
"chart": [{
"label": "emotional distress",
"value": 2
}, {
"label": "peer difficulties",
"value": 2
}, {
"label": "behavioural difficulties",
"value": 4
}, {
"label": "kind and helpful behaviour",
"value": 2
}, {
"label": "overall stress",
"value": 13
}, {
"label": "_sdqID",
"value": 8
}, {
"label": "hyperactivity and concentration",
"value": 3
}]
},
"title": "xxx"
}, {
"contents": {
"chart": [{
"label": "emotional distress",
"value": 5
}, {
"label": "peer difficulties",
"value": 6
}, {
"label": "behavioural difficulties",
"value": 8
}, {
"label": "kind and helpful behaviour",
"value": 7
}, {
"label": "overall stress",
"value": 32
}, {
"label": "_sdqID",
"value": 7
}, {
"label": "hyperactivity and concentration",
"value": 6
}]
},
"title": "xxx"
}, {
"contents": {
"chart": [{
"label": "emotional distress",
"value": 7
}, {
"label": "peer difficulties",
"value": 6
}, {
"label": "behavioural difficulties",
"value": 8
}, {
"label": "kind and helpful behaviour",
"value": 9
}, {
"label": "overall stress",
"value": 34
}, {
"label": "_sdqID",
"value": 6
}, {
"label": "hyperactivity and concentration",
"value": 4
}]
},
"title": "xxx"
}, {
"contents": {
"chart": [{
"label": "emotional distress",
"value": 5
}, {
"label": "peer difficulties",
"value": 4
}, {
"label": "behavioural difficulties",
"value": 4
}, {
"label": "kind and helpful behaviour",
"value": 6
}, {
"label": "overall stress",
"value": 21
}, {
"label": "_sdqID",
"value": 5
}, {
"label": "hyperactivity and concentration",
"value": 2
}]
},
"title": "xxx"
}, {
"contents": {
"chart": [{
"label": "emotional distress",
"value": 1
}, {
"label": "peer difficulties",
"value": 0
}, {
"label": "behavioural difficulties",
"value": 0
}, {
"label": "kind and helpful behaviour",
"value": 0
}, {
"label": "overall stress",
"value": 1
}, {
"label": "_sdqID",
"value": 4
}, {
"label": "hyperactivity and concentration",
"value": 0
}]
},
"title": "xxx"
}]

How to count by attribute in JSON?

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.

Grouping by ID in mongoDB

Can anyone help me with the following aggregate operation in mongodb: having a collection of items with ids and group ids, group them by group ids. For example, for collection of items:
{
"id": 1,
"group_id": 10,
"data": "some_data",
"name": "first"
},
{
"id": 2,
"group_id": 10,
"data": "some_data",
"name": "second"
},
{
"id": 3
"group_id": 20,
"data": "some_data",
"name": "third"
}
Create new collection of groups with the following structure:
{
"id": 10,
"items": [
{
"id": 1,
"group_id": 10,
"data": "some_data",
"name": "first"
},
{
"id": 2,
"group_id": 10,
"data": "some_data",
"name": "second"
}
]
},
{
"id": 10,
"items": [
{
"id": 2,
"group_id": 20,
"data": "some_data",
"name": "third"
}
]
}
The corresponding snippet with Java and spring-data-mongodb will also be appreciated.
In fact I'm doing the same right now with Java and want to move this logic to mongo for paging optimisation.
You can do it with the folowwing simple group aggregation:
db.table.aggregate(
[
{
$group: {
_id : "$group_id",
items : { "$push" : "$$ROOT" }
}
}
]
);
When you want to output the data from the aggregation into a new collection use the $out operator

Categories