Search for Higgs and Z boson decays to J/ψγ and (nS)γ with the ATLAS detector