How can I correctly extract metafields values in JSON code?

I’m not sure where / how you want to filter them out. If you mean access a single value in liquid, my preferred approach is like this:

{%- assign mf_object = product.metafields | where: "namespace", "custom" | where: "key", "color" | first -%}

If you mean in a condition, here’s example example for order metafields: