Need help with custom villager trading [duplicate]

Ok, I've been using a command block to spawn a villager that has custom trades. There's no problem with that at all- I'm just wondering if it's possible to have a villager trade me an item that has a custom name (eg. Key). Or is this unchangeable, so I'd need to name it with an anvil after purchase?

/summon Villager ~ ~1 ~ {Offers:{Recipes:[{buy:{id:"minecraft:emerald",Count:4},sell:{id:"minecraft:leather_helmet",Count:1}},{buy:{id:"minecraft:emerald",Count:6},sell:{id:"minecraft:leather_chestplate",Count:1}},{buy:{id:"minecraft:emerald",Count:5},sell:{id:"minecraft:leather_leggings",Count:1}}]}}

The code is fully functional, it's just integrating custom names for the item he sells.


Solution 1:

NBT you would include in /give commands for items goes inside the "tag" compound. https://minecraft.gamepedia.com/Player.dat_format#Item_structure

/summon Villager ~ ~1 ~ {Offers:{Recipes:[{buy:{id:"minecraft:emerald",Count:4b},sell:{id:"minecraft:leather_helmet",Count:1b,tag:{display:{Name:"Foo"}}}},{buy:{id:"minecraft:emerald",Count:6b},sell:{id:"minecraft:leather_chestplate",Count:1b}},{buy:{id:"minecraft:emerald",Count:5b},sell:{id:"minecraft:leather_leggings",Count:1b}}]}}