aur8l
aur8l

Reputation: 125

Trying to get a JSONArray out of a JSONObject fails

I am writing an API in Java using Spark (irrelevant to my problem but that gives a bit of context). I have the following JSON (exactly as it is returned by my back-end):

{"CfgCampaign":{"callingLists":{"CfgCallingListInfo":{"callingListDBID":{"value":126},"share":{"value":10},"isActive":{"value":2}}},"xmlns":"http://schemas.genesyslab.com/Protocols/Configuration/ConfServer/2005/","DBID":{"value":101},"name":{"value":"WI_Camp_1"},"state":{"value":1},"campaignGroups":{"CfgCampaignGroupInfo":[{"groupType":{"value":5},"dialerDBID":{"value":0},"optMethodValue":{"value":80},"origDNDBID":{"value":0},"numOfChannels":{"value":10},"groupDBID":{"value":826},"isActive":{"value":2},"scriptDBID":{"value":0},"trunkGroupDNDBID":{"value":0},"operationMode":{"value":1},"dialMode":{"value":2},"statServerDBID":{"value":176},"optRecBuffSize":{"value":6},"optMethod":{"value":1},"minRecBuffSize":{"value":4}},{"groupType":{"value":5},"dialerDBID":{"value":0},"optMethodValue":{"value":80},"origDNDBID":{"value":0},"numOfChannels":{"value":10},"groupDBID":{"value":827},"isActive":{"value":2},"scriptDBID":{"value":0},"trunkGroupDNDBID":{"value":0},"operationMode":{"value":1},"dialMode":{"value":2},"statServerDBID":{"value":176},"optRecBuffSize":{"value":6},"optMethod":{"value":1},"minRecBuffSize":{"value":4}}]},"scriptDBID":{"value":0},"tenantDBID":{"value":101}}}

It seems to be valid, as per https://jsonlint.com/ I store it in my code as a JSONObject (so basically, the above is the result of a toString()). However, when I try to extract "campaignGroups" in a JSONArray, I get:

org.json.JSONException: JSONObject["campaignGroups"] not found.

I actually get this error even by just trying to get any key for that matter, e.g. calling get("DBID") will return the same error.

I am a bit confused as to what is going on here, and any help would be appreciated.

edit: Because it is only obvious from the exception, I am using json.org

Thanks !

Upvotes: 0

Views: 214

Answers (2)

Decimix
Decimix

Reputation: 373

I believe the error you're experiencing is due to the outer JSON object, "CfgCampaign", that contains the rest of your data. Some sample code for how to get around this using the org.json library is shown below:

// Loads the JSON (assuming you provide it as a string).
JSONObject x = new JSONObject(...);
// Gets and stores a reference to the outer object.
JSONObject y = x.getJSONObject("CfgCampaign");
// Now you can access any of the nested fields as follows.
JSONObject z = y.getJSONObject("campaignGroups");

Upvotes: 1

Andrew Clavin
Andrew Clavin

Reputation: 574

Calling toString() on that object should return a string that looks something like "[Object Object]", if the toString method has to be called on it, then you'll also need to call JSON.parse(your_object_here) on it to access properties on it.

Also it looks like you'll need to be looking for your_object_name.CfgCampaign.campaignGroups or your_object_name["CfgCampaign"]["campaignGroups", but it's hard to tell without the code you're using to access campaignGroups.

If you set something like

const obj = {"CfgCampaign":{"callingLists":{"CfgCallingListInfo":{"callingListDBID":{"value":126},"share":{"value":10},"isActive":{"value":2}}},"xmlns":"http://schemas.genesyslab.com/Protocols/Configuration/ConfServer/2005/","DBID":{"value":101},"name":{"value":"WI_Camp_1"},"state":{"value":1},"campaignGroups":{"CfgCampaignGroupInfo":[{"groupType":{"value":5},"dialerDBID":{"value":0},"optMethodValue":{"value":80},"origDNDBID":{"value":0},"numOfChannels":{"value":10},"groupDBID":{"value":826},"isActive":{"value":2},"scriptDBID":{"value":0},"trunkGroupDNDBID":{"value":0},"operationMode":{"value":1},"dialMode":{"value":2},"statServerDBID":{"value":176},"optRecBuffSize":{"value":6},"optMethod":{"value":1},"minRecBuffSize":{"value":4}},{"groupType":{"value":5},"dialerDBID":{"value":0},"optMethodValue":{"value":80},"origDNDBID":{"value":0},"numOfChannels":{"value":10},"groupDBID":{"value":827},"isActive":{"value":2},"scriptDBID":{"value":0},"trunkGroupDNDBID":{"value":0},"operationMode":{"value":1},"dialMode":{"value":2},"statServerDBID":{"value":176},"optRecBuffSize":{"value":6},"optMethod":{"value":1},"minRecBuffSize":{"value":4}}]},"scriptDBID":{"value":0},"tenantDBID":{"value":101}}}

then to get the array in campaignGroups you'll need something to look like:

obj.CfgCampaign.campaignGroups

Upvotes: 0

Related Questions