The story's protagonist is a young man named John. After his father dies, John decides to leave home and go out into the world to seek his fortune. Shortly after he pays off the debts of a dead man whom he never met, John is joined on his travels by a man with magical powers. With the help of his traveling companion, John puts a stop to a series of unjust executions and wins the hand of a princess in marriage.
The death of young John's father leaves him without any living relatives. While he is standing at his father's deathbed, John briefly falls asleep. He has a dream in which he is with his father and a beautiful young woman who is wearing a crown. John's father congratulates him on finding a beautiful bride.
After his father's funeral, John decides to travel far away from home to seek his fortune. He takes with him all the money that he has, which consists of fifty rixdollars and a few silver coins. After a few days, John gives all of his silver coins to a beggar.
One stormy night, John takes shelter inside a church. He awakes during the night and sees that there is a coffin in the church. Two men are trying to remove the dead body from the coffin. When John asks the men what they are doing, they say that the dead man owed them money which they now have no possibility of getting back. To get their revenge, they plan to deny the man a decent burial. John says that he will give the two men his fifty rixdollars if they will leave the dead man's body in his coffin. The two men accept John's offer because his fifty rixdollars exactly cover the dead man's debts.
After leaving the church, John walks through a forest. When he comes out of the forest, he is greeted by another man. The man asks John if he can join him on his travels. John happily accepts. Shortly afterwards, John and his traveling companion see an old woman who is gathering firewood. The old woman falls down and breaks her leg. The traveling companion has some ointment with him. The old woman's broken leg is completely mended when some of the ointment is rubbed on it. The only payment that the traveling companion asks for in return is three sticks.
John and his traveling companion stop at an inn where a puppet show is being performed. The show's story involves a king and a queen. Towards the end of the performance, an audience member's dog gets up and bites the queen puppet in two. The traveling companion rubs some of his ointment on the puppet. Not only is the queen puppet fixed, she is now able to move on her own without her strings being pulled. The other puppets beg to have the ointment rubbed on them also. The traveling companion agrees. The only payment he asks for is the puppeteer's sword.
At the top of a mountain, a swan falls down dead at the feet of John and his traveling companion. The traveling companion says that the swan's wings may come in useful. He cuts them off with the puppeteer's sword.
John and his traveling companion arrive in the town where the king and his daughter the princess live. Although the king is a good man, the princess is extremely cruel and is rumored to be a witch. In spite of her cruelty, many men have tried to marry the princess because of her beauty and in the hope of inheriting her father's kingdom after he dies. The princess has declared that she will only marry a man who can correctly guess what she is thinking about three times. Anyone who fails to guess correctly is put to death. When John sees the princess pass through the street, he notices that she looks exactly like the woman he saw in his dream on the evening that his father died. John decides that he will risk death and try to marry the princess.
John goes to the palace and is warmly greeted by the king. The king is saddened, however, when he finds out that John wants to try to win the hand of the princess. The king shows John the princess' garden, which is decorated with the bones of the many men who tried and failed to guess the three things that the princess was thinking about before. Nevertheless, the king says that John can return the next day to guess what the princess is thinking about in front of witnesses. If he guesses correctly and lives, he can return the next day to guess the second object. If he guesses correctly the second time and escapes with his life again, he can return on the third day to guess the final object about which the princess is thinking.
That evening, John is given some punch to drink by his traveling companion. He falls asleep after he drinks it. After John has fallen asleep, his traveling companion puts on the swan's wings and flies to the palace. He sees the princess, who is wearing some black wings, fly out of a window. The traveling companion magically makes himself invisible and follows the princess. As he follows her, the traveling companion strikes the princess with one of the sticks. The princess does not know that the invisible traveling companion is there and thinks that she is being hit by hailstones. The traveling companion follows the princess inside a mountain which is the home of a troll. The troll has cast a spell on the princess, making her wicked and causing her to have so many men killed. The princess tells the troll that another young man is to try to guess what she is thinking about the next day. The troll tells the princess to think about her shoe. He tells her to bring him John's eyes so that he can eat them.
In the morning, the traveling companion tells John that he had a dream abut the princess' shoe. At the palace, John correctly guesses that the princess is thinking about her shoe.
The traveling companion follows the princess to the troll's mountain again. This time, he strikes her with two of the sticks. The traveling companion hears the troll tell the princess to think about her glove. Thanks to his traveling companion, John is able to correctly guess what the princess is thinking about for a second time.
On the third evening, the princess is unhappy because John has managed to escape death twice. To cheer her up, the troll organizes a party for the princess. Afterwards, the troll flies back to the palace with the princess. The traveling companion follows them and strikes them both with the three sticks. Before the troll leaves, he tells the princess to think about his head the next day. The invisible traveling companion takes the troll by surprise and cuts his head off with the puppeteer's sword. He wraps the troll's head in a handkerchief.
In the morning, the traveling companion gives the handkerchief to John. He tells him not to unwrap it until the princess asks him to guess what she is thinking about. When the princess asks John what she is thinking about, he shows her the troll's head. The princess has to admit that John has correctly guessed what she was thinking about three times. She feels obliged to keep her word and marries John the same day. However, she is still under the troll's spell and does not love John. The traveling companion gives John three feathers from the swan's wings and some ointment. When the princess is dipped into a bath which contains the feathers and the ointment, the spell is lifted and she falls in love with John.
John asks his traveling companion to stay with him. The traveling companion says that he could only stay with John until he paid off the debt he owed him. He tells John that he is the dead man whose debts John paid. He then vanishes.