1

Hello i am trying to parse a JSONFILE looking like this :

"ansible_facts": {
    "ansible_all_ipv4_addresses": [
        "xxx"
    ], 
    "ansible_all_ipv6_addresses": [], 
    "ansible_architecture": "xxx", 
    "ansible_bios_date": "xxx", 
    "ansible_bios_version": "xxx", 
    "ansible_cmdline": {
        "KEYBOARDTYPE": "xx", 
        "KEYTABLE": "xx", 
        "LANG": "xxx", 
        "SYSFONT": "xxx", 
        "crashkernel": "xxx", 
        "elevator": "xxx", 
        "nmi_watchdog": "1", 
        "quiet0": true, 
        "rd_LVM_LV": "xxx", 
        "rd_NO_DM": true, 
        "rd_NO_LUKS": true, 
        "rd_NO_MD": true, 
        "rhgb": true, 
        "ro": true, 
        "root": "xxx", 
        "selinux": "0"

...

Here is my Java program :

public static void main(String[] args) {

    JSONParser parser = new JSONParser();

    try {

        String filePath = "C:\\xxx\\xxx\\xxx\\JSON\\file";

        String file = readFileAsString(filePath);

        JSONObject jsonObject = (JSONObject)parser.parse(file);

        JSONArray ansibleFacts = (JSONArray)jsonObject.get("ansible_facts");

        for (Object o : ansibleFacts)
        {
            JSONObject fact = (JSONObject) o;

            String os = (String) fact.get("ansible_architecture");
            System.out.println(os);

            String name = (String) fact.get("ansible_processor_count");
            System.out.println(name);

            String pythonVersion = (String) fact.get("ansible_python_version");
            System.out.println(pythonVersion);
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

}

private static String readFileAsString(String filePath) throws java.io.IOException{
    byte[] buffer = new byte[(int) new File(filePath).length()];
    BufferedInputStream f = null;
    try {
        f = new BufferedInputStream(new FileInputStream(filePath));
        f.read(buffer);
    } finally {
        if (f != null) try { f.close(); } catch (IOException ignored) { }
    }
    return new String(buffer);
}

I get the error : Exception in thread "main" java.lang.ClassCastException: org.json.simple.JSONObject cannot be cast to org.json.simple.JSONArray.

My JSON file starts directly with an ARRAY, i read several posts but can't understand why it doesn't work...

1 Answer 1

1

ansible_facts is not an array whereas ansible_all_ipv4_addresses or ansible_all_ipv6_addresses are arrays.

Some examples for accessing specific elements:

JSONObject ansibleFacts = jsonObject.getJSONObject("ansible_facts");
String architecture = ansibleFacts.getString("ansible_architecture");
JSONArray ipv4Addresses = ansibleFacts.getJSONArray("ansible_all_ipv4_addresses");
String xxx = ipv4Addresses.getString(0);
Sign up to request clarification or add additional context in comments.

3 Comments

But then why is: String architecture = (String) jsonObject.get("ansible_architecture"); System.out.println(architecture); returning null ?
Thanks, but the problem is that i am using simple-jonson library and not the jonson-java library, so with yoru code i got => the method getJSONObject(String) is undefined for the type JSONObject
Libraries can be different but the idea is straightforward. You should have understood how you could access arrays or regular elements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.