We have realized a reversible structure transformation of one-dimensional 1D K4CdCl6-type Sr3CoMO6 with the Co2+/M4+ cation ordering into the two-dimensional 2D double layered Ruddlesden-Popper structure Sr3CoMO 7-δ with a random distribution of Co and M (with M = Ru, Ir) upon increasing the partial oxygen pressure. The combined soft and hard X-ray absorption spectroscopy studies show that under transformation, Co and M cations were oxidized to Co3+ and M5+. During oxidation, high-spin Co2+ in Sr3CoMO6 first transforms into high-spin Co3+ in oxygen-deficient Sr3CoMO 7-δ, and then further transforms into low-spin Co3+ in fully oxidized Sr3CoMO7 upon further increasing the partial pressure of oxygen. The 1D Sr3CoMO6 compound is magnetically ordered at low temperatures with the magnetic moments lying along the c-axis. Their alignment is parallel for Sr3CoRuO6 and antiparallel for Sr3CoIrO6. The 2D compounds reveal a spin-glass-like behavior related to the random distribution of magnetic cations in the structure.