"Only to" vs "Just to"
Consider these two sentences:
I walked into the room only to see whether the window was open.
I walked into the room only to realise I had forgotten my reason for going there.
In the first sentence 'only' has the sense of
Solely, merely, exclusively; with no one or nothing more besides; as a single or solitary thing or fact; no more than. Also, with a verb or verb phrase: no more than, simply, merely (OED).
In this context 'just' works as well as 'only'.
In the second sentence 'only' serves to
emphasize the contrary nature of a consequence (OED). For example
Should I stay..at home, I would only dissipate my little fortune. R. Burns (1787) Let. to J. Smith 11 June
or the OP example.
In this 2nd sentence 'just' is not an alternative because it does not have this meaning.