In a ruling on Thursday morning five Supreme Court justices allowed an appeal brought by several Christian charities.
The flagship policy, introduced by the SNP administration at Holyrood, set out to appoint a single point of contact, such as a teacher or health visitor, to look out for the welfare of children under 18.
Ministers have argued the service will act as a safety net to help families and children if they need it while opponents argue the move breaches the human rights of parents.
Judges, however, agreed with the Christian organisations that the information-sharing provisions proposed under the 2014 Act may result in disproportionate interference with Article 8 rights under the European Convention on Human Rights - the right to a family and private life.
The judgement said: "The first thing that a totalitarian regime tries to do is to get at the children, to distance them from the subversive, varied influences of their families, and indoctrinate them in their rulers' view of the world."
The Supreme Court has given the Scottish Parliament and Scottish ministers "an opportunity to address the matters raised in the judgment".
Deputy First Minister John Swinney said "the attempt to scrap the named person service has failed" and pledged to implement it as soon as possible.
He said ministers would reword the legislation to ensure it is compatible with human rights.
His response showed the Scottish Government was "in denial", said Simon Calvert from the Christian Institute.
Speaking to Premier outside the Supreme Court he said: "The Scottish Government cannot bring the scheme into force in its current form.
"It's a pretty serious business when a government is told by the Supreme Court of the UK that they've passed legislation that breaches the privacy rights and human rights of families and children. It's a big ruling and we're delighted.
"The Scottish Government is in denial. Anyone who's read the judgment knows that we won and they lost - the appeal was allowed.
"The Scottish Government ought to have a period of reflection and a bit of humility, really."
But SNP MSP John Mason told Premier's News Hour: "As a Christian I'm delighted the court has approved the scheme in principle.
"There's going to be some minor tweaking around the edges but I'm really, really pleased because it's going to help vulnerable families, it's going to help children and from a Christian perspective I just think it's great."
Gordon MacDonald from the Christian charity CARE told Premier the scheme had to be "scraped or significantly reformed".
"The scheme was completely disproportionate and a threat to private and family life. It was totalitarian in its nature".
Simon Calvert speaking to Premier's Antony Bushfield:
John Mason MSP speaking to Premier's Alex Williams:
Gordon Macdonald speaking to Premier's Antony Bushfield: