Hostile juxtaposition will let me essentially swap places with another creature when attacked, allowing the attack to damage that creature instead. If the target of my hostile juxtaposition was the one attacking me and I activated it, would the target damage itself? This should work by RAW, and Ican see this working fine with regards to a ranged attacker, but will this allow me to swap places with somebody throwing a punch at me?
"Stop hitting yourself!"
The description of the spell hostile juxtaposition does not say that the creature that's affected by the spell and the creature doing the attacking must be different creatures:
When attacked or the subject of a spell that deals damage to you, you can spend an immediate action to cause yourself and the target creature to teleport and switch places. The target then becomes the target of the triggering attack or spell instead of you.
Thus, according to the rules, a creature can totally stab itself with its sword or be impaled by its own arrow. Even by strictly interpreting the rules for immediate actions, the spell specifically allows the immediate action to be taken upon being attacked but before damage is dealt.
(Note that in such a case the swapped-in creature's attack or spell isn't automatically successful; the creature merely becomes its own target. A swapped-in creature must, for example, still make a successful attack roll against itself or fail a saving throw against its own spell.)
The GM may, however, still say No
Despite those being the rules, having a creature teleporting into its own sword blow may be disconcerting to a GM worried about the magic system's realism. Such a DM may argue, for example, that a creature just can't make an attack roll, teleport, and stab itself: "The spell would have to teleport the parts making the attack after the parts being attacked, and that's not what the spell says it does!" Although this GM finds such a stance on magic difficult, were I player in such a campaign it would take more than that single ruling to force me to leave the table.