NoobCoder
NoobCoder

Reputation: 77

org.json.JSONException: JSONArray[0] is not a JSONObject - Java

Hello I am trying to read and parse a JSON file, when I attempt to read it I got exception of =org.json.JSONException: JSONArray[0] is not a JSONObject. The JSON is shorten for sake of example. Provided will be my code,json and desired output.

Code:

public void Trial () throws JSONException {
        String json = "[[{"appId": "MBSP","askPrice": 0,"bidPrice": 0,"collectionDataSource": "ExternalTick","collectionName": "FRM_MBS_TBA_FN_15Y_0.03_FWD0","collectionObservationTime": "2020-09-21T17:47:59.703Z","collectionType": "LIVE","coupon": 1.03,"createdBy": "Test","createdOn": "2020-09-21T17:47:59.703Z","createdOnDate": 0,"forward": 0,"issuingAgency": "FF","lastUpdated": "2020-09-21T17:47:59.703Z","lastUpdatedBy": "string","lastUpdatedDate": 0,"maturity": ,"midPrice":0 ,"mtaVersionNumber": 0,"settlementDate": "2020-09-21T17:47:59.703Z"}]]
 ";
        JSONArray jsonObj = new JSONArray(json);
        for (int i = 0; i < jsonObj.length(); i++) {
            JSONObject jsonobject = jsonObj.getJSONObject(i);
            String Coupon = jsonobject.getString("Coupon");
            System.out.println(Coupon);
        }
    }

JSON:

[[
  {
    "appId": "MBSP",
    "askPrice": 0,
    "bidPrice": 0,
    "collectionDataSource": "ExternalTick",
    "collectionName": "FRM_MBS_TBA_FN_15Y_0.03_FWD0",
    "collectionObservationTime": "2020-09-21T17:47:59.703Z",
    "collectionType": "LIVE",
    "coupon": 1.03,
    "createdBy": "Test",
    "createdOn": "2020-09-21T17:47:59.703Z",
    "createdOnDate": 0,
    "forward": 0,
    "issuingAgency": "FF",
    "lastUpdated": "2020-09-21T17:47:59.703Z",
    "lastUpdatedBy": "string",
    "lastUpdatedDate": 0,
    "maturity": ,
    "midPrice":0 ,
    "mtaVersionNumber": 0,
    "settlementDate": "2020-09-21T17:47:59.703Z"
  }
]]

Wanted ooutput

 1.03

Any help would be appreciated.

Upvotes: 2

Views: 4541

Answers (3)

Anish B.
Anish B.

Reputation: 16479

The valid JSON should be :

[
  {
    "appId": "MBSP",
    "askPrice": 0,
    "bidPrice": 0,
    "collectionDataSource": "ExternalTick",
    "collectionName": "FRM_MBS_TBA_FN_15Y_0.03_FWD0",
    "collectionObservationTime": "2020-09-21T17:47:59.703Z",
    "collectionType": "LIVE",
    "coupon": 1.03,
    "createdBy": "Test",
    "createdOn": "2020-09-21T17:47:59.703Z",
    "createdOnDate": 0,
    "forward": 0,
    "issuingAgency": "FF",
    "lastUpdated": "2020-09-21T17:47:59.703Z",
    "lastUpdatedBy": "string",
    "lastUpdatedDate": 0,
    "maturity": 0,
    "midPrice":0 ,
    "mtaVersionNumber": 0,
    "settlementDate": "2020-09-21T17:47:59.703Z"
  }
]

Update the code as well :

public class Sample {

    public static void main(String[] args) {
        String json = "[{\n" + 
                "   \"appId\": \"MBSP\",\n" + 
                "   \"askPrice\": 0,\n" + 
                "   \"bidPrice\": 0,\n" + 
                "   \"collectionDataSource\": \"ExternalTick\",\n" + 
                "   \"collectionName\": \"FRM_MBS_TBA_FN_15Y_0.03_FWD0\",\n" + 
                "   \"collectionObservationTime\": \"2020-09-21T17:47:59.703Z\",\n" + 
                "   \"collectionType\": \"LIVE\",\n" + 
                "   \"coupon\": 1.03,\n" + 
                "   \"createdBy\": \"Test\",\n" + 
                "   \"createdOn\": \"2020-09-21T17:47:59.703Z\",\n" + 
                "   \"createdOnDate\": 0,\n" + 
                "   \"forward\": 0,\n" + 
                "   \"issuingAgency\": \"FF\",\n" + 
                "   \"lastUpdated\": \"2020-09-21T17:47:59.703Z\",\n" + 
                "   \"lastUpdatedBy\": \"string\",\n" + 
                "   \"lastUpdatedDate\": 0,\n" + 
                "   \"maturity\": 0,\n" + 
                "   \"midPrice\": 0,\n" + 
                "   \"mtaVersionNumber\": 0,\n" + 
                "   \"settlementDate\": \"2020-09-21T17:47:59.703Z\"\n" + 
                "}]";
        JSONArray jsonObj = new JSONArray(json);
        for (int i = 0; i < jsonObj.length(); i++) {
            JSONObject jsonobject = jsonObj.getJSONObject(i);
            double Coupon = jsonobject.getDouble("coupon");
            System.out.println(Coupon);
        }
    }

}

Output :

1.03

Upvotes: 2

Rony Nguyen
Rony Nguyen

Reputation: 1144

Your JSON input seems invalid:

  • No [[...]]
  • "maturity": , maybe not valid json node
  • code String Coupon = jsonobject.getString("Coupon"); not correct

Solution:

  • Update JSON input like. [...]
  • Update maturity to a valid even value is empty/null
  • Change you code to String coupon = jsonobject.getString("coupon");

Upvotes: 1

if you check again your json you will notice there is array in array and then object. [ [ { } ] ]

Try this input, [ { } ]

[ { "appId": "MBSP", "askPrice": 0, "bidPrice": 0, "collectionDataSource": "ExternalTick", "collectionName": "FRM_MBS_TBA_FN_15Y_0.03_FWD0", "collectionObservationTime": "2020-09-21T17:47:59.703Z", "collectionType": "LIVE", "coupon": 1.03, "createdBy": "Test", "createdOn": "2020-09-21T17:47:59.703Z", "createdOnDate": 0, "forward": 0, "issuingAgency": "FF", "lastUpdated": "2020-09-21T17:47:59.703Z", "lastUpdatedBy": "string", "lastUpdatedDate": 0, "maturity": , "midPrice":0 , "mtaVersionNumber": 0, "settlementDate": "2020-09-21T17:47:59.703Z" } ]

Another way is handle in code to access json array inside a json array to get json object.

Thanks, I hope that helps you.

Upvotes: 1

Related Questions