First, I think it obvious that Jonah did not put this prayer to paper inside the leviathan, but wrote these words sometime later. Second, it isn't obvious who the author of the book itself is. Perhaps Jonah himself wrote it, but we really don't know. Third, it's unavoidable to notice that the book includes details that smack of hyperbole1:
- Jonah sleeping through the storm
- The sailor's response to Jonah's confession
- The big fish that swallows Jonah (of course)
- The king ordering even the animals to be dressed in sackcloth
- Nineveh is said to be "an enormously large city a three days’ walk across."2
- And most amazing of all, the people of Nineveh repent!
I don't bring these up to cast doubt on the historical veracity3 of the story, but to point out that something extraordinary is going on. What is happening is that God seems intent on pushing the boundaries of His mercy beyond the borders and people of Israel and miraculously pulls Jonah (kicking and screaming) to Nineveh to do it.
From the perspective of the Ninevites (to this day), Jonah's miraculous rescue from the depths of the sea could be seen as almost the inverse of the Exodus. Instead of God passing an entire people through the parted sea to make them His people, God passes one man to offer mercy and salvation to an entire city. What's most critical to this question is that Jonah always resists God's intentions at every turn—even when his life is sustained in the belly of the fish.
Within the prayer itself, Jonah is totally focused on his salvation due to his status as a Jew. So verse 5, Jonah laments never being able to see the Temple:
I thought I was driven away
Out of Your sight:
Would I ever gaze again
Upon Your holy Temple?
Verse 8, Jonah emphasizes the Lord's dwelling in Jerusalem:
When my life was ebbing away,
I called the Lord to mind;
And my prayer came before You,
Into Your holy Temple.
Verses 9 and 10, Jonah contrasts the hope of idolaters to the hope of those who sacrifice to the Lord:
They who cling to empty folly
Forsake their own welfare,
But I, with loud thanksgiving,
Will sacrifice to You;
What I have vowed I will perform.
Deliverance is the Lord’s!
The tone of his prayer is so out of step with what God is trying to do, it just begs to be seen as irony: Jonah isn't going to be rescued in order to make sacrifices at the temple but to rescue those "who cling to empty folly". And look again at the final line: Jonah has been delivered from one type of death (the sea) but still faces digestion in the fish. Later, he will face a fate he would prefer death over: bringing about the repentance of Nineveh.
As a literary device, the prayer becomes most powerful when we hear God's final word:
Then the Lord said: “You cared about the plant, which you did not work for and which you did not grow, which appeared overnight and perished overnight. And should not I care about Nineveh, that great city, in which there are more than a hundred and twenty thousand persons who do not yet know their right hand from their left, and many beasts as well!”—Jonah 4:10-11 (NJPS)
God doesn't want sacrifice at the expense of obedience to His command and mercy toward His living creatures. (See for instance 1st Samuel 15:22, Proverbs 21:3, Isaiah 1 etc.)
Summary
Assuming the author of Jonah is Jonah himself, his prayer functions perfectly to highlight the main theme of the book as a whole. It would not be unreasonable to imagine that Jonah composed the prayer sometime after the events of the book. It was informed by his actual thoughts and experience when he was waiting to be vomited out of the leviathan, yet also with the knowledge of the Lord's purpose in Nineveh.
Footnotes:
- I'm grateful to Amichai for his helpful comments in chat where he provided this list.
- Although it could be read as taking three days to go up and down each street. I don't know if the Hebrew grammar could allow for that interpretation.
- It's a very close thing in my mind whether we are intended to read Jonah as a historical account or as a complex parable or allegory. I lean ever so slightly toward the story being historically accurate, but I believe my interpretation works either way.