The decays B+→J/ψπ+π−K+ are studied using a data set corresponding to an integrated luminosity of 9fb−1 collected with the LHCb detector in proton-proton collisions between 2011 and 2018. Precise measurements of the ratios of branching fractions with the intermediate ψ2(3823), χc1(3872) and ψ(2S) states are reported. The decay of B+→ψ2(3823)K+ with ψ2(3823)→Jψπ+π− is observed for the first time with a significance of 5.1 standard deviations. The mass differences between the ψ2(3823), χc1(3872) and ψ(2S) states are measured to be mχc1(3872)−mψ2(3823)mψ2(3823)−mψ(2S)mχc1(3872)−mψ(2S)===47.50±0.53±0.13MeV/c2,137.98±0.53±0.14MeV/c2,185.49±0.06±0.03MeV/c2, resulting in the most precise determination of the χc1(3782) mass. The width of the ψ2(3823) state is found to be below 5.2MeV at 90\% confidence level. The Breit-Wigner width of the χc1(3872) state is measured to be Γχc1(3872)BW=0.96−0.18+0.19±0.21MeV, which is inconsistent with zero by 5.5 standard deviations.