quid pro quo quid pro quo - 4 days ago 6
Java Question

Appending entry into JSON array with JSON-simple

I'm having trouble appending to a JSON file. I can add the new entry but not insert it into the correct position.

Code:

public static void main(String args[]) throws Exception {

{
File file = new File("jsonFormatting.json");
if (!file.exists()) {
System.out.println("No file");
} else {
try {
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("jsonFormatting.json"));
JSONObject jsonObject = (JSONObject) obj;
JSONArray jsonItemInfo = (JSONArray) jsonObject.get("itemInfo");

JSONObject newObject = new JSONObject();

newObject.put("item", new Integer(10003));
newObject.put("name", "ID10003");

StringWriter out = new StringWriter();
newObject.writeJSONString(out);
String jsonText = out.toString();
System.out.println(jsonText);

jsonItemInfo.add(newObject);

FileWriter fileToWrite = new FileWriter("jsonFormatting.json", true);
try {
fileToWrite.write(jsonItemInfo.toJSONString());
} catch (IOException e) {
e.printStackTrace();
}
fileToWrite.flush();
fileToWrite.close();

} catch (IOException | ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}


JSON file:

"sampleArray": [
"Element_0",
"Element_1"
],
"dataPoint_1": 40,
"dataPoint_2": 500,
"dataPoint_3": 650,
"itemInfo": [
{
"item": "10001",
"name": "ID10001",
},
{
"item": "10002",
"name": "ID10002",
}
]


I would like to add the following into the "itemInfo":

{
"item": "10003",
"name": "ID10003",
}


However, when I run my Java code, it adds this to the end of the JSON file, rather than inserting the new entry following the original 2:

[{"item":"10001","name":"ID10001"},{"item":"10002","name":"ID10002"},{"item":10003,"name":"ID10003"}]


Thanks in advance for any advice you may offer!

Answer

I run this code and it is working fine can you test this stuff on your side. If i understand you question correct.

public static void main(String args[]) throws Exception {

            {
                File file = new File("jsonFormatting.json");
                if (!file.exists()) {
                    System.out.println("No file");
                } else {
                    try {
                        JSONParser parser = new JSONParser();
                        Object obj = parser.parse(new FileReader("jsonFormatting.json"));
                        JSONObject jsonObject = (JSONObject) obj;
                        JSONArray jsonItemInfo = (JSONArray) jsonObject.get("itemInfo");

                        JSONObject newObject = new JSONObject();

                        newObject.put("item", new Integer(10003));
                        newObject.put("name", "ID10003");

                        StringWriter out = new StringWriter();
                        newObject.writeJSONString(out);
                        String jsonText = out.toString();
                        System.out.println(jsonText);

                        jsonItemInfo.add(newObject);
                        jsonObject.put("itemInfo", jsonItemInfo);
                        FileWriter fileToWrite = new FileWriter("jsonFormatting.json", false);
                        try {
                            fileToWrite.write(jsonObject.toJSONString());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        fileToWrite.flush();
                        fileToWrite.close();

                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }

my jsonFormatting.json file data look like

{"sampleArray": [
    "Element_0",
    "Element_1"
],
"dataPoint_1": 40,
"dataPoint_2": 500,
"dataPoint_3": 650,
"itemInfo": [
    {
        "item": "10001",
        "name": "ID10001"
    },
    {
        "item": "10002",
        "name": "ID10002"
    }
]
}

and output is

{
    "sampleArray": [
        "Element_0",
        "Element_1"
    ],
    "itemInfo": [
        {
            "item": "10001",
            "name": "ID10001"
        },
        {
            "item": "10002",
            "name": "ID10002"
        },
        {
            "item": 10003,
            "name": "ID10003"
        }
    ],
    "dataPoint_2": 500,
    "dataPoint_1": 40,
    "dataPoint_3": 650
}
Comments