A search is presented for decays of Z and Higgs bosons to a J/ψ meson and a photon, with the subsequent decay of the J/ψ to μ+μ- . The analysis uses data from proton-proton collisions with an integrated luminosity of 35.9 fb-1 at s=13TeV collected with the CMS detector at the LHC. The observed limit on the Z→J/ψγ decay branching fraction, assuming that the J/ψ meson is produced unpolarized, is 1.4×10-6 at 95% confidence level, which corresponds to a rate higher than expected in the standard model by a factor of 15. For extreme-polarization scenarios, the observed limit changes from -13.6 to +8.6% with respect to the unpolarized scenario. The observed upper limit on the branching fraction for H→J/ψγ where the J/ψ meson is assumed to be transversely polarized is 7.6×10-4 , a factor of 260 larger than the standard model prediction. The results for the Higgs boson are combined with previous data from proton-proton collisions at s=8TeV to produce an observed upper limit on the branching fraction for H→J/ψγ that is a factor of 220 larger than the standard model value.