We present a measurement of the Z boson differential cross section in rapidity and transverse momentum using a data sample of pp collision events at a centre-of-mass energy s=8 TeV, corresponding to an integrated luminosity of 19.7 fb-1. The Z boson is identified via its decay to a pair of muons. The measurement provides a precision test of quantum chromodynamics over a large region of phase space. In addition, due to the small experimental uncertainties in the measurement the data has the potential to constrain the gluon parton distribution function in the kinematic regime important for Higgs boson production via gluon fusion. The results agree with the next-to-next-to-leading-order predictions computed with the fewz program. The results are also compared to the commonly used leading-order MadGraph and next-to-leading-order powheg generators. © 2015 CERN for the benefit of the CMS Collaboration.