Fragmentation of jets containing a prompt J/ψ meson in PbPb and pp collisions at 5.02TeV