In my book a character who the main characters have saved before is revealed to be the real main antagonist. I originally wanted this reveal to happen at the end, but after looking at betrayals in most things, I found that they usually happen at the climax. But my question is: When is a good time to have a betrayal?
Boba Fit provides a really good answer.
But there are other times to do so. If you've seen the third episode of Star Wars, the clones turn on the Jedi at the most unexpected moment. You can also have a character commit a betrayal as punishment for insensitivity (which may not be an answer to your question, but I'm still thinking along the Star Wars theme).
It's the Roger Rabbit principle. Roger could only take his hand out of the handcuff when it was funny.
You can only reveal the betrayal when it is interesting. You can use it to drive whatever aspect of things you are doing. Raise tension or character development or fascination with the brave detective, etc.
You work out the plot and decide when it will hold your reader. You work out when to reveal it to John, the betrayed person on the same rule. When will it hold your reader?
There are tons of ways to do it.
You can keep it all secret and have it be the Big Surprise at the end. Sherlock Holmes stories are often much like this. Though, of course, Holmes knew everything as soon as the client showed up at Baker Street.
It can be known to the reader very early, but the betrayed person much later. This can provide tension. When will John figure it out? What will he do when he does? Does he understand the clues you are dropping?
It can be known to everybody early except the reader. John has known for ages, and when the Big Reveal comes, John then drops his secret and demonstrates he has known all along. And the bad-guy is caught. Though don't go too far with that or people will be quoting lines from the end of the Bill and Ted movie.
It can even be known to everybody right from the start. The tension can arise from various intrigues and uncovering of evidence that allows proof. This is sometimes done in police procedural dramas. The valiant detective knows the facts but can't prove it until the end.
In each case, the goal is to make the story interesting. Done well you get something fresh and interesting, even if it's a kind of betrayal story that has been done many many times before.
Personally what I would do is build up the relationship with the character that they're going to betray. It will add a whole new layer of betrayal if the readers actually have a connection with the character. This can happen whenever; if this book is just a one off I would have it in the climax, but if it was a series I would wait to it at the very end, and when the person is revealed have them escape or just cut it off right then and there to make the reader want to hear what happens next.
Having recently watched Batman Begins, I liked the approach they took with Ra's al-Ghul. The movie introduced this nameless guy (played by Liam Neeson) who trained Bruce Wayne (played by Christian Bale) in multiple martial arts and helped him combat his fears and traumatic experiences. He really seemed like a good guy, while Ra's al-Ghul was a different guy who had motives of destroying Gotham City. When Bruce must kill a man to win the favor of al-Ghul, he refuses (as a classic Batman gesture) which leads into this whole fight where the temple gets burned. As the nameless trainer is about to die, Bruce saves his life and leaves, because Bruce valued the teachings he got from that man.
That nameless guy was Ra's al-Ghul the whole time. That happens in the climax of the film, when Bruce realizes that the villain was not Dr. Crane but instead the guy he saved seven years before. The way the movie treats the reveal is a monologue, followed by evil actions, followed by a big fight scene where Bruce chooses not to save al-Ghul this time but instead let him die. This shows how Bruce let go of his memories from the training in order to serve Gotham, following the betrayal he got from his old trainer.
I hope this serves a good example as I think it is better to have the reveal before there is a final battle/fight/whatever. The character should have to face things and decide for themselves after the reveal, so both the protagonist and antagonist can have a resolution. Unless you want a cliffhanger for a next story, it's best to do something after the reveal to finish the story in a way that treats the betrayal properly and leads to a lesson learned (but not a sappy one, it should have some feeling or decision that impacts everything, like Batman letting al-Ghul die comparing to the way he saved him the last time that he was in that situation).