<p>Assuming you've got a controller endpoint in your plugin that has an action, the pseudocode for a simple solution could be something like:</p>

<ul>
<li><p>Grab the JSON data from post.</p></li>
<li><p><a href="http://php.net/manual/en/function.json-decode.php" rel="nofollow noreferrer"><code>json_decode</code></a> it.</p></li>
<li><p>Loop through the JSON data.</p></li>
<li><p>Instantiate a new record and assign the properties to it inside the loop.  </p></li>
</ul>

<p>i.e.</p>

<pre><code>$record = new GeocodeFile_LocationsRecord();
$record-&gt;id = $jsonData['id];
$record-&gt;type = $jsonData['type'];
...
$record-&gt;save();
</code></pre>

CC BY-SA 3.0
2017-07-27T17:18:23.297Z
Thank you for your help with this; it was exactly what I needed.
user3285578
CC BY-SA 3.0
2017-07-19T21:01:01.193Z
2017-07-19T21:01:01.193Z
Brad Bell
<p>I am new to Craft plugin development, so I apologize ahead of time for my lack of understanding. </p>

<p>I'm working on a plugin that allows the user to "upload" Excel/csv files via the control panel that then get sent to an API via Javascript. I then want to save the returned data to my plugin's record. </p>

<p>At the moment, I have a JSON object with all the necessary data and a record set up as such:
    

<pre><code>class GeocodeFile_LocationsRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'geocodefile_locations';
    }

    protected function defineAttributes()
    {
        return array(
            'id'       =&gt; array(AttributeType::Number, 'required' =&gt; true),
            'type'    =&gt; array(AttributeType::Bool, 'default' =&gt; false),
            'name'       =&gt; array(AttributeType::Name, 'default' =&gt; null),
            'address'        =&gt; array(AttributeType::Name, 'default' =&gt; null),
            'city'         =&gt; array(AttributeType::Name, 'default' =&gt; null),
            'state'   =&gt; array(AttributeType::String, 'default' =&gt; null),
            'zip'       =&gt; array(AttributeType::Number, 'default' =&gt; false),
            'url'     =&gt; array(AttributeType::Name, 'default' =&gt; true),
            'lat'   =&gt; array(AttributeType::String, 'default' =&gt; null),
            'lng' =&gt; array(AttributeType::String, 'default' =&gt; null)
        );
    }
}
</code></pre>

<p>Is it possible to save this JSON data to the record directly, or is there a way to create a model in Javascript that could then be saved to the table? Perhaps I can send the data to a service that would handle it instead?</p>

<p>Clearly, I don't know how this is supposed to be handled. Any help with the matter would be greatly appreciated.</p>

<p>Sorry again for my naiveté on this subject! </p>

CC BY-SA 3.0
2017-07-18T21:35:55.790Z
Do you want the JSON object to be stored as JSON in the database?  Or are you looking to match up JSON key => value pairs with column => row pairs in a database table?
Brad Bell
CC BY-SA 3.0
2017-07-19T13:22:56.097Z
I am looking to matchup the key => value pairs to columns and rows. For the way it will be accessed, it would be fine if all data were just strings. The database will probably have around 10,000 entries.
user3285578
<database><javascript><json><models><records>
Saving Form Data to Plugin Record via Javascript
# Saving Form Data to Plugin Record via Javascript

(Asked by: username_0 on 2017-07-18)

I am new to Craft plugin development, so I apologize ahead of time for my lack of understanding. 
I'm working on a plugin that allows the user to "upload" Excel/csv files via the control panel that then get sent to an API via Javascript. I then want to save the returned data to my plugin's record. 
At the moment, I have a JSON object with all the necessary data and a record set up as such:
    

class GeocodeFile_LocationsRecord extends BaseRecord
{
    public function getTableName()
    {
        return 'geocodefile_locations';
    }

    protected function defineAttributes()
    {
        return array(
            'id'       => array(AttributeType::Number, 'required' => true),
            'type'    => array(AttributeType::Bool, 'default' => false),
            'name'       => array(AttributeType::Name, 'default' => null),
            'address'        => array(AttributeType::Name, 'default' => null),
            'city'         => array(AttributeType::Name, 'default' => null),
            'state'   => array(AttributeType::String, 'default' => null),
            'zip'       => array(AttributeType::Number, 'default' => false),
            'url'     => array(AttributeType::Name, 'default' => true),
            'lat'   => array(AttributeType::String, 'default' => null),
            'lng' => array(AttributeType::String, 'default' => null)
        );
    }
}

Is it possible to save this JSON data to the record directly, or is there a way to create a model in Javascript that could then be saved to the table? Perhaps I can send the data to a service that would handle it instead?
Clearly, I don't know how this is supposed to be handled. Any help with the matter would be greatly appreciated.
Sorry again for my naiveté on this subject!

**Tags:** database, javascript, json, models, records
> **Comments:**
> **username_1**: Do you want the JSON object to be stored as JSON in the database?  Or are you looking to match up JSON key => value pairs with column => row pairs in a database table?  
> **username_0**: I am looking to matchup the key => value pairs to columns and rows. For the way it will be accessed, it would be fine if all data were just strings. The database will probably have around 10,000 entries.  

---

## 1 answers

### Answer by username_1 on 2017-07-19. Score: 0 | Accepted answer

Assuming you've got a controller endpoint in your plugin that has an action, the pseudocode for a simple solution could be something like:

Grab the JSON data from post.
json_decode it.
Loop through the JSON data.
Instantiate a new record and assign the properties to it inside the loop.  

i.e.
$record = new GeocodeFile_LocationsRecord();
$record->id = $jsonData['id];
$record->type = $jsonData['type'];
...
$record->save();

> **Comments:**
> **username_0**: Thank you for your help with this; it was exactly what I needed.  

---

