In many cases they can be interchangeable.
However if you want to be extra accurate, knowing the difference can help:
Only - think of the word lonely, it's referring to generally one thing, you only want one of something.
Just - It makes sense the same way you just want one of something, but you can use it like "I just want something to eat"
So you can use it "just" like that <- ;)
Another example is: think of the word justify. "We are just doing the right thing, no need to worry."