Three years later, Detie returns to take Heidi to Frankfurt to be a hired lady's companion to a wealthy girl named Clara Sesemann, who is regarded as an invalid. The girl is charmed by Heidi's simple friendliness, and delights in all the funny mishaps brought about by Heidi's lack of experience with city life. However, the Sesemanns' strict housekeeper, Fräulein Rottenmeier, views the household disruptions as wanton misbehavior, and places Heidi under more and more restraint. Soon, Heidi becomes terribly homesick, and grows alarmingly pale and thin. Her one diversion is learning to read and write, motivated by her desire to go home and read to Peter's blind grandmother. Clara's paternal grandmother comes to visit the children and becomes a friend to Heidi. She teaches Heidi that she can always seek relief from misery by praying to God.
After months pass, the Frankfurt household is brought near hysteria by nightly sightings of what appears to be a ghost. When Clara's father and his friend the doctor keep awake one night to find out what is causing the disturbances, they see that the "ghost" is actually just Heidi, who's sleepwalking in her nightgown. The doctor sees that Heidi's under a great deal of stress. He warns Mr. Sesemann that if Heidi is not sent home promptly, she may become very ill. Soon, a joyous Heidi returns to the mountains, where she teaches her grandfather about the comfort of prayer. She reassures him that it's never too late to turn back to God. Her simple lesson prompts her grandfather to descend to the village and attend a church service for the first time in years, marking an end to his seclusion. He's heartily welcomed back by the church pastor and the villagers.
Heidi and Clara continue to keep in touch and exchange letters. A visit by the doctor to Heidi leads him to eagerly recommend that Clara visit Heidi. He feels assured that the fresh mountain air and the wholesome companionship will do her good. Clara makes the journey again the next season and spends a wonderful summer with Heidi and becomes stronger on goat's milk and fresh mountain air. But Peter, who grows jealous of Heidi and Clara's friendship, pushes her empty wheelchair down the mountain to its destruction (although he later feels guilty about what he did and confesses to it). Without her wheelchair, she attempts to walk and is gradually successful. Her grandmother and father are amazed and overcome with joy to see Clara walking again. The Sesemann family promises to provide permanent care for Heidi, if there ever comes a time when her grandfather is no longer able to do so.