Is this a safe way to extend this wiring?
You did a pretty good job of answering your own question. It will work. Get the right cable to box connectors. You might want to just connect the two junction boxes with some 1/2" conduit and get the appropriate size wire and color from you home store and shove them through the conduit. If all the wire will be copper, use approvd connectors. If the existing cable is aluminum then you'll need the appropriate connectors, like Alumiconn connectors.

Like Alaska Man mentioned above, the junction boxes will have to remain accessible forever.

If it is determined that the locations will not be accessible, then your solution would be to replace the run of cable to locations that are accessible. 

Remember to turn off the power before doing any cutting...