Marco Asensio struck in an extra-time winner as Spain ended Japan's hopes of Olympic gold in men's football with a 1-0 (aet) victory, setting up a clash with Brazil in Saturday's final.
Defending champions Brazil also had to struggle to make the final, beating Mexico 4-1 on penalties after the game ended goalless after extra-time.
Japan had matched Spain throughout the game, playing with creativity and invention going forward and tenacity without the ball and they were five minutes away from taking the match to a penalty shootout.
After a cagey first half, Spain thought they had a great chance to go in front when referee Kevin Ortega of Peru pointed to the penalty spot after Mikel Merino went down under challenge from Maya Yoshida.
But the video assistant referee ordered Ortega to go to the pitch side monitor and he saw the clear images that Yoshida had made a perfectly fair and well-timed challenge.
That overturned penalty sparked both sides into life and it was a lively second half with openings at both ends.
Japan keeper Kosei Tani did well to race out quickly and smother Rafa Mir as the ball broke to him close to goal while at the other end Takefusa Kubo's blast at the near post was parried by Unai Simon.
Both teams were unable to make the most of half-chances, however, and the game followed a similar pattern into extra-time before Real Madrid forward Asensio picked the ball up in the inside right channel, glanced up and curled the ball into the far corner.
Spain are looking to repeat their gold medal success from the Barcelona Games in 1992 but will have to beat the Rio winners from five years ago.
"They are a quality side, we know that and we know that if we are going to win the gold medal we are going to have to play the best teams here," said Spain defender Eric Garcia.
Brazil beat Mexico on penalties to reach final
Earlier, Brazil went close to settling the match inside 90 minutes when Richarlison struck the post with a glancing header in the 82nd minute.
But when it came to the shootout, they had their keeper Santos to thank for a fine save to keep out Eduardo Aguirre's spot-kick and after Mexico's Johan Vasquez hit the post, Brazil just need to keep their cool.
Reiner's right-footed shot into the bottom corner ensured victory for the team captained by 38-year-old former Barcelona full back Dani Alves.
Brazil will face the winners of Tuesday's other semi-final between Spain and Japan in Saitama.