Split Data in JSON File Based on a Key Value Using Java
Question
Source:https://stackoverflow.com/questions/70389908/split-data-in-json-file-based-on-a-key-value-using-java
I have a JSON file that has an array of product items and I want to split them based on the category of the item. This is my JSON file:
{
"items":[
{
"item-id": 123,
"name": "Cheese",
"price": 8,
"category": "Dairy"
},
{
"item-id": 124,
"name": "Milk",
"price": 23,
"category": "Dairy"
},
{
"item-id": 125,
"name": "Chicken",
"price": 100,
"category": "Meat"
},
{
"item-id": 126,
"name": "Fish",
"price": 45,
"category": "Meat"
}
]
}
i want to split them like like this,
[
{
"category":"Dairy",
"items":[
{
"item-id": 123,
"name": "Cheese",
"price": 8,
"category": "Dairy"
},
{
"item-id": 124,
"name": "Milk",
"price": 23,
"category": "Dairy"
}
]
},
{
"category":"Meat",
"items":[
{
"item-id": 125,
"name": "Chicken",
"price": 100,
"category": "Meat"
},
{
"item-id": 126,
"name": "Fish",
"price": 45,
"category": "Meat"
}
]
}
]
this is the code i tried so far but can't find way to split them like the way i wanted, I'm using java and also i am new to java
import java.io.*;
import java.util.*;
import org.json.simple.*;
import org.json.simple.parser.*;
public class ReadOrderDetails {
@SuppressWarnings("unused")
public static void main(String[] args) {
JSONParser parser = new JSONParser();
JSONObject subOrder = new JSONObject();
JSONArray gitems = new JSONArray();
JSONArray subOrders = new JSONArray();
try {
Object obj = parser.parse(new FileReader("order-details.json"));
JSONObject jsonObject = (JSONObject)obj;
String orderId = (String)jsonObject.get("orderId");
JSONArray items = (JSONArray)jsonObject.get("items");
@SuppressWarnings("rawtypes")
Iterator iterator = items.iterator();
System.out.println("Order Id:" + orderId);
while(iterator.hasNext()) {
JSONObject item = (JSONObject)iterator.next();
if(subOrders.isEmpty()) {
subOrder.put("category", item.get("category"));
gitems.add(item);
subOrder.put("items", gitems);
subOrders.add(subOrder);
} else {
Iterator subOrdersIterator = subOrders.iterator();
for(int i=0; i<subOrders.size(); i++) {
JSONObject sitem = (JSONObject) subOrdersIterator.next();
if(sitem.get("category") == item.get("category")) {
gitems.add(item);
subOrder.put("items", gitems);
subOrders.add(subOrder);
} else {
subOrder.put("category", item.get("category"));
gitems.add(item);
subOrder.put("items", gitems);
subOrders.add(subOrder);
}
}
}
}
} catch(Exception e) {
e.printStackTrace();
}
System.out.println(subOrders);
}
}
I'm getting an error at java.util.ConcurrentModificationException, but that's not my main question, what I really wanted is a way to split them. I tried a couple of things but they didn't work.
Answer
Here you want to group the JSON data by category under items array. The code will be very long if you try to get this done in Java. Just try using SPL, an open-source Java package, to do this. You only need one line of code:
A |
|
1 |
=json(file("data.json").read()).items.group(#4;~:items) |
SPL offers JDBC driver to be invoked by Java. Just store the above SPL script as group.splx and invoke it in a Java application as you call a stored procedure:
…
Class.forName("com.esproc.jdbc.InternalDriver");
con= DriverManager.getConnection("jdbc:esproc:local://");
st = con.prepareCall("call group()");
st.execute();
…
View SPL source code.
SPL Official Website 👉 https://www.scudata.com
SPL Feedback and Help 👉 https://www.reddit.com/r/esProc_SPL
SPL Learning Material 👉 https://c.scudata.com
SPL Source Code and Package 👉 https://github.com/SPLWare/esProc
Discord 👉 https://discord.gg/cFTcUNs7
Youtube 👉 https://www.youtube.com/@esProc_SPL
Chinese version