To temporarily resolve this issue, many owners tried pushing the "tow" button on the key fob before locking the car, it effectively deactivates the alarm and prevents it from sounding unexpectedly especially in the middle of the night.
I’ve seen this come up before. Before replacing anything, try checking if the hood latch is properly aligned, it might not be pressing the switch firmly. Also, a zip tie or some padding on the switch can help as a temporary fix. If you’re not comfortable fiddling with it, a local mechanic should be able to sort it out quickly and cheaply.