Answers.Body: <p>In addition to <a href="https://craftcms.stackexchange.com/questions/21864/how-to-save-matrix-data-programmatically-in-craft-3">How to save Matrix data programmatically in Craft 3</a> </p>
  
  <p>You'll <a href="https://stackoverflow.com/questions/19758954/get-data-from-json-file-with-php">fetch your data</a> loop each row and fetch the element / create a new element if none exists</p>
  
  <pre><code>$section = Craft::$app-&gt;sections-&gt;getSectionByHandle('news');
  $entryTypes = $section-&gt;getEntryTypes();
  $entryType = reset($entryTypes);
  foreach ($jsonData as $row) {
      // try to fetch an element
      $element = Craft::$app-&gt;getElements()-&gt;getElementById($row['id']);
      // no element exists, create a new one
      if ($element === null) {
          $element = new Entry(
              [
                  'sectionId'     =&gt; $section-&gt;id,
                  'typeId'        =&gt; $entryType-&gt;id,
                  'fieldLayoutId' =&gt; $entryType-&gt;fieldLayoutId,
                  'authorId'      =&gt; 1,
                  'title'         =&gt; 'My Entry',
                  'slug'          =&gt; 'my-entry',
                  'postDate'      =&gt; new DateTime(),
              ]
          );
      }
      // get the field by handle
      /** @var \craft\fields\Matrix $field */
      $field = Craft::$app-&gt;getFields()-&gt;getFieldByHandle('matrix');
  
      // get the existing matrix blocks
      $existingMatrix = $element-&gt;getFieldValue('matrix');
  
      // serialize the data
      $serializedMatrix = $field-&gt;serializeValue($existingMatrix, $element);
  
     // loop your json matrix blocks
      foreach ($row['yourMatrixBlocks'] as $key =&gt; $block) {
          // append your new Matrix fields
          $serializedMatrix['new' . $key] = [
              'type'   =&gt; 'block',
              'fields' =&gt; [
                  'field' =&gt; $block['field']
              ],
          ];
      }
  
      // set the new field value
      $element-&gt;setFieldValue('matrix', $serializedMatrix);
      $success = Craft::$app-&gt;getElements()-&gt;saveElement($element);
  
      // display errors - just in Case
      if($success === false){
          Craft::dd($element-&gt;getErrors());    
      }
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T08:22:17.037Z
Answers.Text: Excellent! I was hoping it was not beyond me, I am so far away from being a dev, I am a digital marketer filling in to get this working.  My Matrix field has a Table field in it too.  I have to see what I can do with it, but thank you!
Answers.UserDisplayName: Vin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T08:27:35.530Z
Answers.Text: The easiest way to get the data structure for the required field type is to take a look at your  `$_POST` variable when you save an element in your CP.
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T08:32:13.473Z
Answers.Text: Makes sense, I have current  entries I need to update so gotta see with spending some time taking a go at it.
Answers.UserDisplayName: Vin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T08:52:16.213Z
Answers.Text: I have around 10,000 records to import in, so thinking this may be a solution best suited for FeedMe if I can get it to work.
Answers.UserDisplayName: Vin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T08:56:54.227Z
Answers.Text: You can change the code in FeedMe as well if you want
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T09:08:11.403Z
Answers.Text: I was thinking to do that or at least try, but not sure why the current Matrix entries would be deleted when new entries to that field are added.  Doesn't make sense.
Answers.UserDisplayName: Vin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T09:36:43.753Z
Answers.Text: It does, because you overwrite the field value.. When you insert a new title for an entry you'll overwrite it. It does not make any sense to attach the new string to the old one
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T09:51:20.153Z
Answers.Text: I am trying to simple update the current entry with new data into that Mateix field, create more rows and leave the old ones be.  So to me in my non-dev thinking it should always append.
Answers.UserDisplayName: Vin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T11:24:03.030Z
Answers.Text: This does not make sense because you won't ever be able to delete blocks this way. How are you going to remove matrix blocks? Your non-dev thinking is not really accurate
Answers.UserDisplayName: Robin Schambach
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T17:58:58.953Z
Answers.Text: Yes it is accurate to me, your dev thinking is wrong to me, haha.  I say you can go into the entry and delete the blocks and add, but importing should append only.
Answers.UserDisplayName: Vin
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-04T13:49:45.137Z
Answers.Text: Personally, both methods are valid, and there should be an option for how to handle this. The massive issue here, it what happens if you import a feed multiple times? You'll always be appending blocks to the Matrix field. But, it is the current expected behaviour to overwrite the contents of Matrix fields on import - just like it does with every other field.
Answers.UserDisplayName: crawf
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-02T08:10:20.703Z
Answers.LastActivityDate: 2018-05-02T08:16:22.197Z
Answers.LastEditDate: 2018-05-02T08:16:22.197Z
Answers.UserDisplayName: Robin Schambach
Body: <p>When I import using JSON with FeedMe Pro and need to import more records into an entry that has a Matrix field. I find it deletes the previous and adds the new data only.  Is there anyway to append to the Matrix field leaving the previous data intact?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-02T07:37:45.800Z
Comments.Text: The easiest solution: create the import by yourself. Importing entries doesn't take longer than 2-3 minutes. If you want I can show you how
Comments.UserDisplayName: Robin Schambach
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2018-05-02T07:45:37.653Z
Comments.Text: That would be great Robin if you could show me please.
Comments.UserDisplayName: Vin
Tags: <craft3><json><plugin-feedme>
Title: FeedMe Pro Import JSON
ThreadText: # FeedMe Pro Import JSON
  
  (Asked by: username_0 on 2018-05-02)
  
  When I import using JSON with FeedMe Pro and need to import more records into an entry that has a Matrix field. I find it deletes the previous and adds the new data only. Is there anyway to append to the Matrix field leaving the previous data intact?
  
  **Tags:** craft3, json, plugin-feedme
  
  > **Comments:**
  > **username_1**: The easiest solution: create the import by yourself. Importing entries doesn't take longer than 2-3 minutes. If you want I can show you how  
  > **username_0**: That would be great username_1 if you could show me please.  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2018-05-02. Score: 1
  
  In addition to [How to save Matrix data programmatically in Craft 3](https://craftcms.stackexchange.com/questions/21864/how-to-save-matrix-data-programmatically-in-craft-3)
  
  You'll [fetch your data](https://stackoverflow.com/questions/19758954/get-data-from-json-file-with-php) loop each row and fetch the element / create a new element if none exists
  
  ```
  $section = Craft::$app->sections->getSectionByHandle('news');
  $entryTypes = $section->getEntryTypes();
  $entryType = reset($entryTypes);
  foreach ($jsonData as $row) {
      // try to fetch an element
      $element = Craft::$app->getElements()->getElementById($row['id']);
      // no element exists, create a new one
      if ($element === null) {
          $element = new Entry(
              [
                  'sectionId'     => $section->id,
                  'typeId'        => $entryType->id,
                  'fieldLayoutId' => $entryType->fieldLayoutId,
                  'authorId'      => 1,
                  'title'         => 'My Entry',
                  'slug'          => 'my-entry',
                  'postDate'      => new DateTime(),
              ]
          );
      }
      // get the field by handle
      /** @var \craft\fields\Matrix $field */
      $field = Craft::$app->getFields()->getFieldByHandle('matrix');
  
      // get the existing matrix blocks
      $existingMatrix = $element->getFieldValue('matrix');
  
      // serialize the data
      $serializedMatrix = $field->serializeValue($existingMatrix, $element);
  
     // loop your json matrix blocks
      foreach ($row['yourMatrixBlocks'] as $key => $block) {
          // append your new Matrix fields
          $serializedMatrix['new' . $key] = [
              'type'   => 'block',
              'fields' => [
                  'field' => $block['field']
              ],
          ];
      }
  
      // set the new field value
      $element->setFieldValue('matrix', $serializedMatrix);
      $success = Craft::$app->getElements()->saveElement($element);
  
      // display errors - just in Case
      if($success === false){
          Craft::dd($element->getErrors());    
      }
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Excellent! I was hoping it was not beyond me, I am so far away from being a dev, I am a digital marketer filling in to get this working.  My Matrix field has a Table field in it too.  I have to see what I can do with it, but thank you!  
  > **username_1**: The easiest way to get the data structure for the required field type is to take a look at your  `$_POST` variable when you save an element in your CP.  
  > **username_0**: Makes sense, I have current  entries I need to update so gotta see with spending some time taking a go at it.  
  > **username_0**: I have around 10,000 records to import in, so thinking this may be a solution best suited for FeedMe if I can get it to work.  
  > **username_1**: You can change the code in FeedMe as well if you want  
  > **username_0**: I was thinking to do that or at least try, but not sure why the current Matrix entries would be deleted when new entries to that field are added.  Doesn't make sense.  
  > **username_1**: It does, because you overwrite the field value.. When you insert a new title for an entry you'll overwrite it. It does not make any sense to attach the new string to the old one  
  > **username_0**: I am trying to simple update the current entry with new data into that Mateix field, create more rows and leave the old ones be.  So to me in my non-dev thinking it should always append.  
  > **username_1**: This does not make sense because you won't ever be able to delete blocks this way. How are you going to remove matrix blocks? Your non-dev thinking is not really accurate  
  > **username_0**: Yes it is accurate to me, your dev thinking is wrong to me, haha.  I say you can go into the entry and delete the blocks and add, but importing should append only.  
  > **username_2**: Personally, both methods are valid, and there should be an option for how to handle this. The massive issue here, it what happens if you import a feed multiple times? You'll always be appending blocks to the Matrix field. But, it is the current expected behaviour to overwrite the contents of Matrix fields on import - just like it does with every other field.  
  
  ---
  
  