'Error class net.minidev.json.JSONObject cannot be cast to class org.json.JSONArray on parsing the json file

I am trying to automate restapi. However, I am getting a class cast exception error on running the code.

json:

{
  "argument": {
    "ContinueWithWarningsToken": "string",
    "Orders": [
      {
        "OrderId": "20220321000002",
        "AmountType": "BaseCurrency",
        "BaseCurrency": "EUR",
        "ExpiryTime": 360000000,
        "ModelPortfolio": "CURXCOMMON",
        "NominalLots": 100,   
        "Portfolio": "S6007-LUA",
        "PortfolioGroup": "F-S6007LU",
        "OrderFreeCode3": "F",
        "PriceCurrency": "USD",
        "Security": "FX FWD",
        "MaturityDate": "2022-03-21"
      }
    ]
  }
}

Using below code:

File jsonDataInFile = new File("src/test/resources/data/orderCreationBegin.json");
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader("src/test/resources/data/orderCreationBegin.json"));
System.out.println(obj.toString());
JSONArray orderjason = (JSONArray) obj;

I get:

java.lang.ClassCastException: class net.minidev.json.JSONObject cannot be cast to class org.json.JSONArray (net.minidev.json.JSONObject and org.json.JSONArray are in unnamed module of loader 'app')
    at com.nordea.gtaf.testprocedures.APIOrderCreation.apiOrderCreationPortfolioOrders(APIOrderCreation.java:62)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:564)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source