Preferably check it on another Switch and the right joycon of another Switch on yours to determine whether it is the joycon or the Switch that s giving you problems.
Be absolutely sure before going into the Switch itself.
First thing to try is to completely remove the right joycon from the settings menu and they insert it again.
With some luck, that fixes the issue sometimes.
If you're out of luck, take a flashlight and check for bend or missing pins on the Switch.
Or maybe some dirt or gunk stuck in the rails.
Some 99% isopropyl alcohol can be used for cleaning.
Anything else might cause corrosion.
The joycon rails has a ribbon that might have a crack in it, or it might not be seated correctly in the connector.
Cracks can occur due to aging or when the Switch was modded.
The joycon connects almost directly to the APU/CPU so if your Switch was modded, check for scratches on the PCB.
jkyoho already made show measurements around the connector
View attachment 514234
More info here :
https://github.com/dekuNukem/Nintendo_Switch_Reverse_Engineering