In general, rationalization is:
... a defense mechanism in which controversial behaviors or feelings
are justified and explained in a seemingly rational or logical manner
to avoid the true explanation, and are made consciously tolerable—or
even admirable and superior—by plausible means.
The specific case mentioned in the question is one of Sour Grapes, from Aesop's fables:
The story concerns a fox that tries to eat grapes from a vine but
cannot reach them. Rather than admit defeat, he states they are
"Defense mechanism" however, is a psychoanalytic (Freudian) construct, which is generally considered pseudoscience or outdated. The more modern theory of cognitive dissonance now subsumes what was previously thought of as rationalization:
In practice, people reduce the magnitude of their cognitive dissonance
... by changing the conflicting cognition [or] Ignore or deny
information that conflicts with existing beliefs.