In a case of mistaken identity, a New Jersey middle school teacher spent three days in jail after a cop told him during a traffic stop that he was wanted for failing to appear in court on a theft charge, according to a report.

John McCarthy, 56, a teacher at Goetz Middle School in Jackson Township, was arrested in Lakewood on Oct. 21 and informed that there was a warrant out for his arrest in Burlington County, his attorney Thomas Mallon told NJ.com.

But the warrant listed a Jon McCarthy — without the “h” — who has a different date of birth, Mallon told the news outlet.

Adding to the mix-up, the teacher’s driver’s license photo, not the real Jon McCarthy booking photo, was attached to the arrest warrant.

“Burlington made the initial mistake, the Lakewood officer didn’t notice, but should have picked up on it and the Ocean County Jail didn’t initially catch it,” Mallon said.

The teacher repeatedly told authorities they had the wrong man, but police brushed him off, he said.

“Everybody pleads with the police when they get arrested,” Mallon said. “A lot of people say, ‘It wasn’t me.’ But when it really isn’t you, they are still going to say, ‘Sure, it isn’t you.’”

After the arrest, Lakewood police called their Burlington counterparts and asked if deputies could come to pick up the suspect.

“Burlington said they didn’t have anyone available,” so officers took McCarthy to the Ocean County Jail, where he sat for three days, Mallon said.

“He called his wife and his parents and they were all very upset,” he said, adding that word spread at Goetz Middle School that he was in the slammer.

On Oct. 24, a corrections officer recognized McCarthy from the school, Mallon said.

“Luckily, for my client, this was somebody whose child he taught,” Mallon said.

But after spending three days at the Ocean County Jail, McCarthy’s reputation was already damaged because his mug shot taken in the joint quickly circulated among students, Mallon said.

A screenshot of the teacher’s booking photo also was posted to a website that states he was arrested for theft of credit cards, according to NJ.com.

After being sprung, McCarthy immediately reported the fiasco to school district officials.

“It soon became very clear to them that he had been a victim” of mistaken identity, Mallon said.

A jail official has since apologized to McCarthy, but he hasn’t heard from the two police departments, Mallon said, adding that his client has filed notices of intent to sue the law enforcement agencies.

A Burlington police spokesman referred The Post to the county solicitor’s office, where a woman who identified herself as “Jane Doe” declined comment due to pending litigation and hung up.

A message left with Lakewood police was not immediately returned.

“If Ocean County hadn’t discovered the error, who knows how long my client would have sat in jail,” Mallon said.