Aims. In this paper we investigate the detectability of the molecular oxygen in icy dust grain mantles towards astronomical objects. Methods. We present a systematic set of experiments with O2-H2O ice mixtures designed to disentangle how the molecular ratio affects the O2 signature in the mid- and near-infrared spectral regions. All the experiments were conducted in a closed-cycle helium cryostat coupled to a Fourier transform infrared spectrometer. The ice mixtures comprise varying thicknesses from 8 × 10−3 to 3 μm. The absorption spectra of the O2-H2O mixtures are also compared to the one of pure water. In addition, the possibility to detect the O2 in icy bodies and in the interstellar medium is discussed. Results. We are able to see the O2 feature at 1551 cm−1 even for the most diluted mixture of H2O : O2 = 9 : 1, comparable to a ratio of O2/H2O = 10 % which has already been detected in situ in the coma of the comet 67P/Churyumov-Gerasimenko. We provide an estimate for the detection of O2 with the future mission of the James Webb Space Telescope (JWST).