Yes, it will work fine.
It will need new timing chains, cassettes and tensioners, '97 to about '03/'04 4.0l SOHC engines had the older design which failed often, newer design works much better.
Also on the 4.0l SOHC it is a good idea to replace the Knock Sensor, they are hard to get at, and do need replacing.
Check the thermostat housing carefully, they are prone to cracking and leaking.
And replace rear main seal, but that's for anytime any engine is out.
There were 4.0l SOHCs made with an internal balancer shaft, these were used in 4WD vehicles with "Live" front axles, always engaged.
People have swapped these both ways with no problems with vibrations, so no worries on that part.