Search code examples
javajsonarraysjson-simple

Create JSON file with deep array


I need to create new json file with the following structre I am not able to success since i have structure inside structure/array ,the file should look like : any idea how to create one like this?

 {

    "Entry1": [
        {
            "id": "0001",
            "type": "USER",

        },
        {
            "id": "0002",
            "type": "EMP",
        "property":"Salery",

        }

],

"Entry2": [

    {
        "id": "0005",
        "name": "Vacation",
        "property":"user",

    },
    {
        "id": "0008",
        "name": "Work",

        }
    ]

}

I was tried to use the following code without success.

JSONObject obj = new JSONObject();
obj.put("entry1", null);

JSONArray list = new JSONArray();
list.add("id");
list.add("USER");

....


Solution

  • [UPDATE] I've edited my answer because it should return different json. I don't know if this is the bes solution but it works and also it is clear to read and maintain.

    According to Anand answer here is an simple example using Gson. I think that it's great solution because all you need to do is to create POJO and serialize it to json using Gson/Jackson. You don't need to create json object manually.

    JsonTest main class:

    import java.util.ArrayList;
    
    import com.google.gson.Gson;
    
    public class JsonTest {
    
        public static void main(String[] main) {
            Entry entry1 = new Entry();
            entry1.setId(1);
            entry1.setType("USER");
            entry1.setProperty("Salary");
            Entry entry2 = new Entry();
            entry2.setId(2);
            entry2.setType("EMP");
            Entry entry3 = new Entry();
            entry3.setId(2);
            entry3.setType("EMP");
            entry3.setProperty("Work");
            Entry entry4 = new Entry();
            entry4.setId(2);
            entry4.setType("EMP");
    
            EntryListContainer entryListContainer = new EntryListContainer();
            ArrayList<Entry> entryList1 = new ArrayList<Entry>();
            ArrayList<Entry> entryList2 = new ArrayList<Entry>();
    
            entryList1.add(entry1);
            entryList1.add(entry2);
            entryList2.add(entry3);
            entryList2.add(entry4);
    
            entryListContainer.setEntryList1(entryList1);
            entryListContainer.setEntryList2(entryList2);
    
            String json = new Gson().toJson(entryListContainer, EntryListContainer.class);
            System.out.println(json);
        }
    
    }
    

    EntryListContainer class:

    import java.util.ArrayList;
    
    import com.google.gson.annotations.SerializedName;
    
    public class EntryListContainer {
    
        @SerializedName("Entry1")
        private ArrayList<Entry> entryList1;
        @SerializedName("Entry2")
        private ArrayList<Entry> entryList2;
    
        public void setEntryList1(ArrayList<Entry> entryList1) {
            this.entryList1 = entryList1;
        }
    
        public ArrayList<Entry> getEntryList1() {
            return entryList1;
        }
    
        public void setEntryList2(ArrayList<Entry> entryList2) {
            this.entryList2 = entryList2;
        }
    
        public ArrayList<Entry> getEntryList2() {
            return entryList2;
        }
    
    }
    

    Entry POJO:

    public class Entry {
    
        private int id;
        private String type;
        private String property;
    
        public void setId(int id) {
            this.id = id;
        }
    
        public int getId() {
            return id;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        public String getType() {
            return type;
        }
    
        public String getProperty() {
            return property;
        }
    
        public void setProperty(String property) {
            this.property = property;
        }
    
    }
    

    It will return this json:

    {
    
        "Entry1":[
            {
                "id":1,
                "type":"USER",
                "property":"Salary"
            },
            {
                "id":2,
                "type":"EMP"
            }
        ],
        "Entry2":[
            {
                "id":2,
                "type":"EMP",
                "property":"Work"
            },
            {
                "id":2,
                "type":"EMP"
            }
        ]
    
    }