How to use datatags in trades? [duplicate]

You can put a tag-tag into the sell-tag of the trade. Your command may look like this:

/summon minecraft:villager <coordinates> {
  Invulnerable:1b,
  PersistenceRequired:1b,
  Offers:
  {
    Recipes:
    [
      {
        maxUses:8,
        buy:{id:"minecraft:emerald",Count: 1b},
        buyB:{id:"minecraft:air",Count:1b},
        sell:{
          tag: {CanPlaceOn:["minecraft:stone","minecraft:iron_ore"]},
          id:"minecraft:stone",
          Count: 1b}
      },
      {
        maxUses:8,
        buy:{id:"minecraft:emerald",Count: 1b},
        buyB:{id:"minecraft:air",Count: 1b},
        sell:{
          tag:{CanDestroy:["minecraft:stone","minecraft:iron_ore"]},
          id:"minecraft:diamond_pickaxe",
          Count: 1b
        }
      }
    ]
  }
}

The Invulnerable-tag makes the villager invulnerable, the only way to hurt/kill it is by a player in creative mode, or if it falls out of the world into the void.

The PersistenceRequired-tag prevents the villager from despawning, I´m not sure if that is nessessary though.

In the Offers-tag, you have the maxUses-tag, which defines how often you can trade a certain offer, before the villager has to stock up
buy and buyB tags are the items that the villager buys from you
sell is the items that the villager sells to you. To add a CanDestroy, or CanPlaceOn to the item, you use the tag-tag.

From the villager from the example you can get stone that you can place on other stone blocks, or on iron ore, and a diamond pickaxe that you can use to break stone, and iron ore.

Note: The villager will have different trades after it changes profession