Java looping over object - java

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"
}]

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 Format Key/Meta data of the JSON Object in PHP

Given the following code snippet:
<?php
require "db_conn.php";
$sql = "SELECT category,product_code,product_name,unit,quantity FROM items INNER JOIN categories ON items.category_id=categories.id";
$res = $conn->query($sql)->fetchAll();
$structured = [];
if ($res) {
foreach ($res as $key => $row)
{
$structured[$row['category']]['items'][] = [
'product_code' => $row['product_code'],
'product_name' => $row['product_name'],
'unit' => $row['unit'],
'quantity' => $row['quantity'],
];
}
echo json_encode([$structured]);
}
outputs the following JSON response:
[{
"BABY ITEMS": {
"items": [{
"product_code": "151128",
"product_name": "BOUNCY BABY WIPES 80'S",
"unit": "CARTON",
"quantity": "5.00"
}]
},
"CONFECTIONS\/PASTRIES": {
"items": [{
"product_code": "130570",
"product_name": "NUVITA FAMILY 75G",
"unit": "CARTON",
"quantity": "1.00"
}, {
"product_code": "115165",
"product_name": "NUVITA MAGIK LEMON CRM 60*2'S",
"unit": "CARTON",
"quantity": "2.00"
}]
},
"HOUSEHOLD ITEMS": {
"items": [{
"product_code": "150278",
"product_name": "BOUNCY BABY DIAPER 10'S MINI",
"unit": "CARTON",
"quantity": "1.00"
}]
}
}]
How do I modify this part of code:
$structured[$row['category']]['items'][]
to output:
"category": "BABY ITEMS",
instead of:
"BABY ITEMS":
The expected JSON is (Handwritten):
[{
"category": "BABY ITEMS",
"items": [{
"product_code": "151128",
"product_name": "BOUNCY BABY WIPES 80'S",
"unit": "CARTON",
"quantity": "5.00"
}]
},
{
"category": "CONFECTIONS/PASTRIES",
"items": [{
"product_code": "130570",
"product_name": "NUVITA FAMILY 75G",
"unit": "CARTON",
"quantity": "1.00"
},
{
"product_code": "115165",
"product_name": "NUVITA MAGIK LEMON CRM 60*2'S",
"unit": "CARTON",
"quantity": "3.00"
},
{
"product_code": "115160",
"product_name": "NUVITA MAGIK S.BERRY CRM 60*2'S",
"unit": "CARTON",
"quantity": "2.00"
}
]
},
{
"category": "HOUSEHOLD ITEMS",
"items": [{
"product_code": "150278",
"product_name": "BOUNCY BABY DIAPER 10'S MINI",
"unit": "CARTON",
"quantity": "1.00"
}]
},
{
"category": "PERSONAL CARE",
"items": [{
"product_code": "160200",
"product_name": "ALL TYME ULTRA REGULAR 8'S",
"unit": "CARTON",
"quantity": "2.00"
},
{
"product_code": "160309",
"product_name": "ALL TYME ULTRA MEDIUM 8'S",
"unit": "CARTON",
"quantity": "4.00"
},
{
"product_code": "160235",
"product_name": "GOLDEN SHOE POLSIH 50ML BLACK",
"unit": "CARTON",
"quantity": "1.00"
},
{
"product_code": "190251",
"product_name": "ALL TYME ULTRA MEDIUM 16'S",
"unit": "CARTON",
"quantity": "2.00"
}]
}
]
I really need the JSON formatted as above as am using it to create a nested category and items RecyclerView. This is how am parsing in android java:
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject objCategory = jsonArray.getJSONObject(i);
System.out.println("Category: " + objCategory.getString("category"));
// fetch item details and store it in arraylists
JSONArray itemArray = objCategory.getJSONArray("items");
//System.out.println("Item length: " + itemArray.length());
ParentItem category = new
ParentItem(objCategory.getString("category"),OrderItemsList(itemArray));
orderFetchingList.add(category);
}
Please see the following code (fully functional). I mocked $res with few records and copied your code to fill $structured. Below that, please find my code to restructure the data in order the get the desired JSON output
$res = [
[
'category' => 'BABY',
'product_code' => '123',
'product_name' => 'BABY STUFF',
'unit' => 1,
'quantity' => 999,
],
[
'category' => 'BABY',
'product_code' => '125',
'product_name' => 'BABY EAT',
'unit' => 1,
'quantity' => 999,
],
[
'category' => 'MEN',
'product_code' => '124',
'product_name' => 'MEN STUFF',
'unit' => 2,
'quantity' => 888,
]
];
echo '<pre>';
$structured = [];
foreach($res as $key => $row) {
$structured[$row['category']]['items'][] = [
'product_code' => $row['product_code'],
'product_name' => $row['product_name'],
'unit' => $row['unit'],
'quantity' => $row['quantity'],
];
}
print_r($structured);
// restructure the data to get the desired JSON output
$collection = [];
foreach($structured as $category => $items) {
$collection[] = [
'category' => $category,
'items' => $items['items']
];
}
echo json_encode($collection);
Output (JSON):
[
{
"category": "BABY",
"items": [
{
"product_code": "123",
"product_name": "BABY STUFF",
"unit": 1,
"quantity": 999
},
{
"product_code": "125",
"product_name": "BABY EAT",
"unit": 1,
"quantity": 999
}
]
},
{
"category": "MEN",
"items": [
{
"product_code": "124",
"product_name": "MEN STUFF",
"unit": 2,
"quantity": 888
}
]
}
]

How to Iterate through Json schema and values based on keys

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?

Find value in JSON HTTP response

I want to get the value of city from the below httpResponse (JSON Response) where fruit is Apple. I am unable to find a way to add this condition to my groovy script.
{
"userInformation": {
"Name": "John",
"Location": "India"
},
"details": [
{
"fruit": "Apple",
"color": "Red",
"city": "New Delhi",
"luckyNumber": 10
},
{
"fruit": "Banana",
"color": "yellow",
"city": "Goa",
"luckyNumber": 12
}
]
}
JsonSlurper is what you need. It parses JSON object to a plain Map which can be then easily navigated to find the desired value.
import groovy.json.JsonSlurper
def input = '''{
"userInformation": {
"Name": "John",
"Location": "India"
},
"details": [
{
"fruit": "Apple",
"color": "Red",
"city": "New Delhi",
"luckyNumber": 10
},
{
"fruit": "Banana",
"color": "yellow",
"city": "Goa",
"luckyNumber": 12
}
]
}
'''
def slurped = new JsonSlurper().parseText(input)
slurped.details.find { it.fruit == 'Apple' }?.city

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.

Categories