Updated:

A Mending book by version

VersionCommand
Java 1.21.5+/give @p minecraft:enchanted_book[stored_enchantments={mending:1}]
Java 1.20.5–1.21.4/give @p minecraft:enchanted_book[stored_enchantments={levels:{mending:1}}]
Java 1.13–1.20.4/give @p minecraft:enchanted_book{StoredEnchantments:[{id:"minecraft:mending",lvl:1s}]}

1.20.5 moved items to components, and 1.21.5 removed the levels layer. The wrong format gives an error or a blank book.

Several enchantments

/give @p minecraft:enchanted_book[stored_enchantments={efficiency:5,unbreaking:3,mending:1}]

Separate them with commas (1.21.5+). On 1.20.5–1.21.4 put the same list inside levels:{…}; on 1.20.4 and earlier add one {id:…,lvl:…s} entry per enchantment.

stored_enchantments vs enchantments

Books need stored_enchantments. A book made with enchantments glows but its enchantments are not stored, so an anvil cannot transfer them. To enchant a tool directly, skip the book and use the enchant command generator.

Common enchantment IDs

EnchantmentIDMax
MendingmendingI
UnbreakingunbreakingIII
EfficiencyefficiencyV
FortunefortuneIII
Silk Touchsilk_touchI
SharpnesssharpnessV
LootinglootingIII
ProtectionprotectionIV

Using the book on an anvil

Common problems

Check your command

The Command Error Doctor checks common syntax mistakes in your browser.