I just put a 4.0 in my 4.2 YJ. It is not that easy. I ended up using the entire harness from the donor vehicle. At the very least you will need his harness from the firewall forward ( he can use yours to hack up to make his jeep run )
That being said..it is a mess. In order to get the CEL and other warning lights correct, it is much easier to use his complete interior harness. You will also need his fuel tank, or you will need to install an aftermarket fuel pump.
In short.... if your 4.2 runs, take his 4.0, put it on a stand, and buy a 4.0 YJ with a blown engine and a rusted frame. Then do the swap.