Try this, hope this is what looking for
if ($product->getCustomAttributes()) {
$attribute_value = $product->getCustomAttribute('attribute_code') ? $product->getCustomAttribute('attribute_code')->getValue() : '';
return [
'msg' => $attribute_value ,
];$attribute_value;
}
Try it and do let me know. Peace :)