public JSONArray buildSpotifyJsonObject(ArrayList<Trip> list) { JSONArray array = new JSONArray(); try { for (Trip trip : list) { JSONArray tracks = new JSONArray(); for (Track track : trip.getTracks()) { tracks.put( new JSONObject() .put("name", track.getName()) .put("artist", track.getArtist()) .put("duration", track.getDuration()) .put("url", track.getUrl())); } array.put( new JSONObject() .put("origin", trip.getOrigin()) .put("destination", trip.getDest()) .put("id", trip.getId()) .put("duration", trip.getDurationMs()) .put("originDate", trip.getOriginDate()) .put("destinationDate", trip.getDestDate()) .put("originTime", trip.getOriginTime()) .put("destinationTime", trip.getDestTime()) .put("playlist", tracks)); } } catch (Exception E) { } System.out.println(array); return array; }