javajsonjson-simple

JSON structure not being recognised as JSONObject or JSONArray


I'm having difficulty in parsing my json file into something that I can work with. The json file looks like this:

{
    "characterizations_values": [
        {
            "filename": "TestDoc",
            "sheet": {
                "my-title": [
                    {
                        "number": "ddddko9",
                        "numbered-part": "test number",
                        "documents": [
                            [
                                {
                                    "document_name": "20 Minutes",
                                    "document-category": [
                                        "Accounting",
                                        "Cost-Management"
                                    ]
                                }
                            ]
                        ] 
                    }
                ],
                "my-notes": [
                    {
                        "dimensions": {
                            "enclosing": {
                                "test-one": 34.33,
                                "test-two": 34
                            },
                            "enclosing-extra": {
                                "test-one": 27.33,
                                "test-two": 88
                            }
                        }
                    }
                ]
            }
        }
    ]
}

What I've attempted with the library json-simple is the following:

import org.json.simple.JSONObject;
import org.json.simple.JSONArray;

                Map<String, String> curValues = (Map<String,String>) curBlock.get("characterizations_values");
                
                // break down sheet
                JSONObject sheet = new JSONObject();
                sheet.put("sheet", curValues.get("sheet"));
                LOG.info(String.format("sheet: %s", sheet.toString()));

                // Get my title     
                JSONObject myTitle = new JSONObject();
                myTitle.put("title_blocks", sheet.get("my-title"));
                
                // Get my notes
                JSONObject myNotes = new JSONObject();
                myNotes.put("my-notes", sheet.get("my-notes"));

This doesn't however work and while I get a json object with a key I get a null value for the value. I've also attempted to cast this into a JSONArray but then I get an error message saying that "java.util.LinkedHashMap cannot be cast to..". After reading a few posts I understand that the library doesn't have enough information to deserialise the object. But given the structure, I think that I would first need to get a JSON object and then single out the JSONArray. So the order would be:

  1. get my-title JSONObject
  2. get value of mytitle which is a JSONArray
  3. Keep working down the document path

If there is a better way to do this I'm very much open to suggestions.

Thanks in advance!


Solution

  • If you use an online parser, like for example, jsonViewer you could see that your JSON is valid.

    The library seems a false problem: while there are many better libraries that exists to parse JSON, json-simple while old could be a candidate at least to execute the task.

    JSON file data is in composed of name/value pairs.

    Data is separated by commas, curly braces hold objects, square brackets hold arrays and values could be of primitive type.

    Here we have a single JSON Object named "characterizations_values" which holds an Array with one element.

    A better solution would be to get the root element first while accessing the leafs from there.

        JSONParser jsonParser = new JSONParser();
        try (FileReader reader = new FileReader("yourfile.json"))
        {
            Object obj = jsonParser.parse(reader);
            JSONObject documentList = (JSONObject) obj;
        } catch (ParseException e) {
            e.printStackTrace();
        }