  A search is presented for decays of Z and Higgs bosons to a J$/\psi$ meson
and a photon, with the subsequent decay of the J$/\psi$ to $\mu^+\mu^-$. The
analysis uses data from proton-proton collisions with an integrated luminosity
of 35.9 fb$^{-1}$ at $\sqrt{s} =$ 13 TeV collected with the CMS detector at the
LHC. The observed limit on the Z $\to$ J$/\psi \gamma$ decay branching
fraction, assuming that the J$/\psi$ meson is produced unpolarized, is 1.4
$\times$ 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
$\to$ J$/\psi \gamma$ where the J$/\psi$ meson is assumed to be transversely
polarized is 7.6 $\times$ 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 $\sqrt{s} =$ 8 TeV to produce an observed
upper limit on the branching fraction for H $\to$ J$/\psi \gamma$ that is a
factor of 220 larger than the standard model value.
