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!

  • 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
    Commented Jul 18, 2017 at 21:35
  • 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. Commented Jul 19, 2017 at 13:22

1 Answer 1


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.


$record = new GeocodeFile_LocationsRecord();
$record->id = $jsonData['id];
$record->type = $jsonData['type'];
  • Thank you for your help with this; it was exactly what I needed. Commented Jul 27, 2017 at 17:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.