That is exactly right.
The purpose of this arrangement is to put the mains voltage on one side of the "Great Wall Of Separation", and the low voltage on the other, as called out by the Code requirements for (not) mixing low voltage and mains.
However, as Tyson points out, the junction box must remain accessible and have a cover. I assume this is unimproved space, or you are putting a box extension on it to raise it to the finished wall surface. Burying the entire junction box and transformer behind drywall, that is no-go.