Answers.Body: <p>You're close!</p>
  
  <p>The method that Lindsey recommends should work with the following method of adding content to a field:</p>
  
  <pre><code>$entry-&gt;getContent()-&gt;setAttributes(array(
      'tableFieldHandle' =&gt; json_encode($tableData)
  ));
  </code></pre>
  
  <p>However, the <a href="https://github.com/boboldehampsink/import#modifyimportrow"><code>modifyImportRow</code></a> documentation uses the (arguably better) method:</p>
  
  <pre><code>// Set modified content
  $element-&gt;setContentFromPost($content);
  </code></pre>
  
  <p>Using this method, you should add an <code>array</code> to a table field, rather than a <code>JSON</code> strong, since <code>setContentFromPost()</code> will call the relevant methods to format the field data appropriately for the database, so you don't need to worry about it.</p>
  
  <p>So if you use:</p>
  
  <pre><code>$content[$handle] = $tabledata;
  </code></pre>
  
  <p>then add it to the element using:</p>
  
  <pre><code>// Set modified content
  $element-&gt;setContentFromPost($content);
  </code></pre>
  
  <p>it should work.</p>
  
  <hr>
  
  <p>The full code for the <code>modifyImportRow</code> function in your plugin should now look like this:</p>
  
  <pre><code>/**
   * Modify data just before importing
   *
   * @param BaseElementModel $element  The current element receiving import data.
   * @param array            $map      Mapping of data between CSV -&gt; Craft fields.
   * @param array            $data     Raw data provided for this row.
   */
  public function modifyImportRow($element, $map, $data)
  {
      // Map data to fields
      $fields = array_combine($map, $data);
  
      // Initialize content array
      $content = array();
  
      // Arrange your content in a way that makes sense for your plugin
      foreach ($fields as $handle =&gt; $value) {
  
          if ($handle=="myTableHandle"){
  
              $rows = explode(',', $value);
  
              $tabledata = array();
  
              foreach ($rows as $row) {
                  $rowarray = explode(':',$row);
  
                  $rowdata['col1'] = $rowarray[0];
                  $rowdata['col2'] = $rowarray[1];
  
                  $tabledata[] = $rowdata;
              }
  
          }
  
          $content[$handle] = $tabledata;
  
      }
  
      // Set modified content
      $element-&gt;setContentFromPost($content);
  }
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-08T14:08:34.800Z
Answers.Text: Yep. That does it. Much obliged.
Answers.UserDisplayName: Steve Mock
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-08-08T13:45:02.190Z
Answers.LastActivityDate: 2016-08-08T15:22:07.823Z
Answers.LastEditDate: 2016-08-08T15:22:07.823Z
Answers.UserDisplayName: Steve Rowling
Body: <p>Importing <code>data.csv</code> using Bob Olde Hampsink's <a href="https://github.com/boboldehampsink/import" rel="nofollow noreferrer">Import</a> plugin. Given that <code>data.csv</code> has a 'table' item string thusly:</p>
  
  <pre><code>"Alcohol and Drug Treatment:RX-8450.3300,Counseling:RP-1400.8000-145,Mental Health Services:RR-5150.1500,Support Groups:PNPH-8100.4500-050"
  </code></pre>
  
  <p>(two columns, four rows. dead simple)</p>
  
  <p>Lindsay tells us <a href="https://craftcms.stackexchange.com/questions/6889/inserting-data-into-a-table-fieldtype-via-plugin">here</a> that: </p>
  
  <blockquote>
    <p>a table field contains a simple <strong>JSON string</strong></p>
  </blockquote>
  
  <p>So we are writing a plugin using Import's <a href="https://github.com/boboldehampsink/import#modifyimportrow" rel="nofollow noreferrer">modifyImportRow</a> hook and here's what we have in the hook loop:</p>
  
  <pre><code>...
  // Arrange your content in a way that makes sense for your plugin
  foreach ($fields as $handle =&gt; $value) {
  
      if ($handle=="myTableHandle"){
  
        $rows = explode(',', $value);
  
        $tabledata = array();
  
        foreach ($rows as $row) {
          $rowarray = explode(':',$row);
  
          $rowdata['col1'] = $rowarray[0];
          $rowdata['col2'] = $rowarray[1];
  
          $tabledata[] = $rowdata;
        }
  
        $value = json_encode($tabledata);
  
     }
  
     $content[$handle] = $value;
  
  }
  ...
  </code></pre>
  
  <p>The import works, but the table field data (myTableHandle) remains NULL.</p>
  
  <p>What else can we do to that string of table data to get the Import plugin to accept?</p>
  
Tags: <table-field><plugin-import>
Title: Why won't the Import plugin accept this JSON string?
ThreadText: # Why won't the Import plugin accept this JSON string?
  
  (Asked by: username_0 on 2016-08-08)
  
  Importing `data.csv` using Bob Olde Hampsink's [Import](https://github.com/boboldehampsink/import) plugin. Given that `data.csv` has a 'table' item string thusly:
  
  ```
  "Alcohol and Drug Treatment:RX-8450.3300,Counseling:RP-1400.8000-145,Mental Health Services:RR-5150.1500,Support Groups:PNPH-8100.4500-050"
  
  ```
  
  (two columns, four rows. dead simple)
  
  Lindsay tells us [here](https://craftcms.stackexchange.com/questions/6889/inserting-data-into-a-table-fieldtype-via-plugin) that:
  
  > a table field contains a simple **JSON string**
  
  So we are writing a plugin using Import's [modifyImportRow](https://github.com/boboldehampsink/import#modifyimportrow) hook and here's what we have in the hook loop:
  
  ```
  ...
  // Arrange your content in a way that makes sense for your plugin
  foreach ($fields as $handle => $value) {
  
      if ($handle=="myTableHandle"){
  
        $rows = explode(',', $value);
  
        $tabledata = array();
  
        foreach ($rows as $row) {
          $rowarray = explode(':',$row);
  
          $rowdata['col1'] = $rowarray[0];
          $rowdata['col2'] = $rowarray[1];
  
          $tabledata[] = $rowdata;
        }
  
        $value = json_encode($tabledata);
  
     }
  
     $content[$handle] = $value;
  
  }
  ...
  
  ```
  
  The import works, but the table field data (myTableHandle) remains NULL.
  
  What else can we do to that string of table data to get the Import plugin to accept?
  
  **Tags:** table-field, plugin-import
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2016-08-08. Score: 6 | Accepted answer
  
  You're close!
  
  The method that Lindsey recommends should work with the following method of adding content to a field:
  
  ```
  $entry->getContent()->setAttributes(array(
      'tableFieldHandle' => json_encode($tableData)
  ));
  
  ```
  
  However, the [`modifyImportRow`](https://github.com/boboldehampsink/import#modifyimportrow) documentation uses the (arguably better) method:
  
  ```
  // Set modified content
  $element->setContentFromPost($content);
  
  ```
  
  Using this method, you should add an `array` to a table field, rather than a `JSON` strong, since `setContentFromPost()` will call the relevant methods to format the field data appropriately for the database, so you don't need to worry about it.
  
  So if you use:
  
  ```
  $content[$handle] = $tabledata;
  
  ```
  
  then add it to the element using:
  
  ```
  // Set modified content
  $element->setContentFromPost($content);
  
  ```
  
  it should work.
  
  ---
  
  The full code for the `modifyImportRow` function in your plugin should now look like this:
  
  ```
  /**
   * Modify data just before importing
   *
   * @param BaseElementModel $element  The current element receiving import data.
   * @param array            $map      Mapping of data between CSV -> Craft fields.
   * @param array            $data     Raw data provided for this row.
   */
  public function modifyImportRow($element, $map, $data)
  {
      // Map data to fields
      $fields = array_combine($map, $data);
  
      // Initialize content array
      $content = array();
  
      // Arrange your content in a way that makes sense for your plugin
      foreach ($fields as $handle => $value) {
  
          if ($handle=="myTableHandle"){
  
              $rows = explode(',', $value);
  
              $tabledata = array();
  
              foreach ($rows as $row) {
                  $rowarray = explode(':',$row);
  
                  $rowdata['col1'] = $rowarray[0];
                  $rowdata['col2'] = $rowarray[1];
  
                  $tabledata[] = $rowdata;
              }
  
          }
  
          $content[$handle] = $tabledata;
  
      }
  
      // Set modified content
      $element->setContentFromPost($content);
  }
  
  ```
  
  > **Comments:**
  > **username_0**: Yep. That does it. Much obliged.  
  
  ---
  
  