Exract all node field values to json including reference entities with value I am writing a module to export drupal's content to another platform, the module provides a UI to manage content types and their fields to be shared on another platform via API.
So far i have managed to get node field values using this sample code
$nid = 4951;
$node = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;load($nid);
$data = [];
foreach ($node-&gt;getFields() as $name =&gt; $field) {
  $data[$name]  = $field-&gt;getString();
}

the $field-&gt;getString(); however do not provides reference entity values and only provide ids.
The out of the above code is like:
{&quot;nid&quot;:&quot;4951&quot;,&quot;uuid&quot;:&quot;0fae7e9b-d860-43f0-9559-a8bd6bab22b7&quot;,&quot;vid&quot;:&quot;1272611&quot;,&quot;langcode&quot;:&quot;en&quot;,&quot;type&quot;:&quot;article&quot;,&quot;revision_timestamp&quot;:&quot;1612303794&quot;,&quot;revision_uid&quot;:&quot;1166&quot;,&quot;revision_log&quot;:&quot;&quot;,&quot;status&quot;:&quot;1&quot;,&quot;uid&quot;:&quot;866&quot;,&quot;title&quot;:&quot;xyz Chapter April Update&quot;,&quot;created&quot;:&quot;1555961981&quot;,&quot;changed&quot;:&quot;1612303794&quot;,&quot;promote&quot;:&quot;0&quot;,&quot;sticky&quot;:&quot;0&quot;,&quot;default_langcode&quot;:&quot;1&quot;,&quot;revision_default&quot;:&quot;1&quot;,&quot;revision_translation_affected&quot;:&quot;1&quot;,&quot;moderation_state&quot;:&quot;published&quot;,&quot;scheduled_transition_date&quot;:&quot;&quot;,&quot;scheduled_transition_state&quot;:&quot;&quot;,&quot;metatag&quot;:&quot;&quot;,&quot;path&quot;:&quot;\/wisconsin-chapter-april-update, 17276, en&quot;,&quot;menu_link&quot;:&quot;&quot;,&quot;body&quot;:&quot;some this here, rich_text&quot;,&quot;field_article_author&quot;:&quot;Wisconsin Chapter&quot;,&quot;field_article_chapter&quot;:&quot;541&quot;,&quot;field_article_endnotes&quot;:&quot;&quot;,&quot;field_article_questions_cta&quot;:&quot;1&quot;,&quot;field_article_recommendation&quot;:&quot;&quot;,&quot;field_article_recommended_link&quot;:&quot;&quot;,&quot;field_article_related&quot;:&quot;&quot;,&quot;field_article_related_link&quot;:&quot;&quot;,&quot;field_article_resource_download&quot;:&quot;&quot;,&quot;field_article_subhead&quot;:&quot;Here's the latest news from the abc.&quot;,&quot;field_article_thumbnail&quot;:&quot;&quot;,&quot;field_article_type&quot;:&quot;news&quot;,&quot;field_blog_section&quot;:&quot;&quot;,&quot;field_meta_tags&quot;:&quot;&quot;,&quot;field_tax_article_type&quot;:&quot;24, 28, 45&quot;,&quot;field_tax_audience&quot;:&quot;&quot;,&quot;field_tax_breadcrumbs&quot;:&quot;&quot;,&quot;field_tax_content_format&quot;:&quot;50&quot;,&quot;field_tax_disease&quot;:&quot;55, 56, 57, 58&quot;,&quot;field_tax_ga_type&quot;:&quot;&quot;,&quot;field_tax_research_type&quot;:&quot;&quot;,&quot;field_tax_topic&quot;:&quot;90, 91, 92, 93, 94, 108, 109, 111&quot;}

Scroll last and see for example field_tax_topic
Is there a direct or indirect way of automatically detecting reference entity field and extracting its values, also it should be dynamic solution and that must work with new field types as well.
Any pointer or direction to do it in the right way will help!
If you really really want to do this which you probably wont with JSON API Views and
serialisation
https://www.drupal.org/docs/8/api/serialization-api/serialization-api-overview
eg:
dump(Drupal::service('serializer')-&gt;serializer-&gt;serialize($entity, 'json'));

But if you really want to ...
$node = \Drupal::entityTypeManager()-&gt;getStorage('node')-&gt;load($nid);
$data = [];
foreach ($node-&gt;getFields() as $name =&gt; $field) {
   // Get Type.
   $field_type = $node-&gt;get($name)-&gt;getFieldDefinition()-&gt;getType();
   //see https://drupal.stackexchange.com/questions/180373/how-to-get-the-list-of-field-types

   switch($field_type) {
      case 'text_with_summary':
      case 'text':
      case 'text_long: 
        $data[$name]  = $field-&gt;getString();
        break;
      case 'entity_reference':
      case 'entity_reference':
        $value = [];
        $items = $field-&gt;referencedEntities();
        if (!empty($items)) {
            foreach ($items as $item) {
                // here is the crappy part 
                $val_1 = $item-&gt;get('field_something')-&gt;value;
                $value[$item-&gt;id()] =  $val_1,
            }
        }
        $data[$name] = $value;
      break;
      // continue for other data types
      // and set a default !
   }
}