add JSONArray within a JSONObject

I'm making an application to send notifications using OneSignal and must do a POST request in JSON format.

To send notification to a user I have to use the include_player_ids argument which have to be an array because it is possible to send to multiple users the same notification (in my case I only send notification to one user). I use a JSONArray to create this array but when adding it to the JSONObject, there are extra quotes for the field include_player_ids.

What I have:

  "headings": {"en":"my_title"},
  "contents": {"en":"my_text"},
  "include_player_ids": "[\"my-player-id\"]",

As you can see, there is some quote around the array [ ].

I guess it's what is making the response error from OneSignal : errors":["include_player_ids must be an array"]

What I want :

"include_player_ids": ["my-player-id"] 

It is weird because adding JSONObject to JSONObject doesn't do this even though it is quite similar as you can see with the headings / contents fields

My code :

import org.json.JSONException;
import org.json.JSONObject;
import org.json.alt.JSONArray;

JSONObject headings = new JSONObject();
JSONObject contents = new JSONObject();
JSONArray player_id = new JSONArray();
JSONObject notification = new JSONObject();
try {
    notification.put("app_id", appId);
    notification.put("include_player_ids", player_id);
    headings.put("en", "my_title");
    contents.put("en", "my_text");
    notification.put("headings", headings);
    notification.put("contents", contents);             

} catch (JSONException e) {
    System.out.println("JSONException :" + e.getMessage());

idUser is a String

Thanks in advance for any help,

Jon Skeet

I believe the problem is that you're using org.json.alt.JSONArray instead of org.json.JSONArray. I'm not familiar with that class, but I suspect JSONObject.put is just calling toString() on it rather than treating it as an existing JSON array. Here's a short but complete example that doesn't have the problem:

import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray; // Note the import here

public class Test {
    public static void main(String[] args) throws JSONException {
        JSONArray playerIds = new JSONArray();
        JSONObject notification = new JSONObject();
        notification.put("include_player_ids", playerIds);




See more on this question at Stackoverflow