The 40 weeks pregnant woman has diagnosed schizoaffective disorder.
She has been detained under the terms of mental health legislation but wanted to give birth naturally.
However, specialists said a natural delivery would be risky and could result in the baby being stillborn.
Mr Justice Francis has ruled the woman lacks the mental capacity to make decisions about treatment for herself.
She was under the care of Chelsea and Westminster Hospital NHS Foundation Trust.