title: Sequelize - How can I return JSON objects of the database results only?
body: <p>So I'm wanting to have the database results returned and nothing else. At the moment I'm getting returned a large chunk of JSON data (Like below):</p>
  
  <p>But I'm only needing the [dataValues] attribute. I don't want to have to use the this bit of <code>JSON</code> to retrieve it: <code>tagData[0].dataValues.tagId</code>.</p>
  
  <p>I just noticed: When it finds and <strong>DOESN'T CREATE</strong>, it will return the <code>JSON</code> for the database results, but when it <strong>DOESN'T FIND</strong> and creates, it returns the un-needed JSON blob (Like below) Is there a way around this?</p>
  
  <pre><code>[ { dataValues:
       { tagId: 1,
         tagName: '#hash',
         updatedAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT),
         createdAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT) },
      _previousDataValues:
       { tagId: 1,
         tagName: '#hash',
         createdAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT),
         updatedAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT) },
      _changed:
       { tagId: false,
         tagName: false,
         createdAt: false,
         updatedAt: false },
      '$modelOptions':
       { timestamps: true,
         instanceMethods: {},
         classMethods: {},
         validate: {},
         freezeTableName: true,
         underscored: false,
         underscoredAll: false,
         paranoid: false,
         whereCollection: [Object],
         schema: null,
         schemaDelimiter: '',
         defaultScope: null,
         scopes: [],
         hooks: {},
         indexes: [],
         name: [Object],
         omitNull: false,
         sequelize: [Object],
         uniqueKeys: [Object],
         hasPrimaryKeys: true },
      '$options':
       { isNewRecord: true,
         '$schema': null,
         '$schemaDelimiter': '',
         attributes: undefined,
         include: undefined,
         raw: true,
         silent: undefined },
      hasPrimaryKeys: true,
      __eagerlyLoadedAssociations: [],
      isNewRecord: false },
    true ]
  </code></pre>
  
  <p>Instead of getting the big blob like the above I only need the <code>RAW</code> json results (Like below):</p>
  
  <pre><code>{ tagId: 1,
         tagName: '#hash',
         updatedAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT),
         createdAt: Fri Dec 25 2015 17:07:13 GMT+1100 (AEDT) },
  </code></pre>
  
  <p>I've used the following javascript. I did try add <code>raw: true</code>, but it didn't work?</p>
  
  <pre><code>    // Find or create new tag (hashtag), then insert it into DB with photoId relation
  module.exports = function(tag, photoId) {
      tags.findOrCreate( { 
          where: { tagName: tag },
          raw: true
      })
      .then(function(tagData){
          // console.log("----------------&gt; ", tagData[0].dataValues.tagId);
          console.log(tagData);
          tagsRelation.create({ tagId: tagData[0].dataValues.tagId, photoId: photoId })
          .then(function(hashtag){
              // console.log("\nHashtag has been inserted into DB: ", hashtag);
          }).catch(function(err){
              console.log("\nError inserting tags and relation: ", err);
          });
      }).catch(function(err){
          if(err){
              console.log(err);
          }
      });
  
  }
  </code></pre>
  
  <h3>Edit:</h3>
  
  <p>So I've investigated a bit and it seems that the big <code>JSON</code> blob is only returned when <code>Sequelize</code> is creating and not finding.</p>
  
  <p>Is there a way around this or not?</p>
  
  <h3>Edit 2:</h3>
  
  <p>Okay so I've found a workaround, which could be turned into a re-usable function. But if there's something built into <code>Sequelize</code>, I'd prefer to use that.</p>
  
  <pre><code>var tagId = "";
  
  // Extract tagId from json blob
  if(tagData[0].hasOwnProperty('dataValues')){
      console.log("1");
      tagId = tagData[0].dataValues.tagId;
  } else {
      console.log("2");
      console.log(tagData);
      tagId = tagData[0].tagId;
  }
  
  console.log(tagId);
  tagsRelation.create({ tagId: tagId, photoId: photoId })
  </code></pre>
  
  <h3>Edit 3:</h3>
  
  <p>So, I don't think there is an "Official" sequelize way of achieving this so I simply wrote a custom module which returns the <code>JSON</code> data that's needed. This module can be customised and extended to suit various situations! If anyone has any suggestions to how the module can be improved feel free to comment :)</p>
  
  <p>In this module we're returning a Javascript Object. If you want to turn it into <code>JSON</code> just stringify it using <code>JSON.stringify(data)</code>. </p>
  
  <pre><code>// Pass in your sequelize JSON object
  module.exports = function(json){ 
      var returnedJson = []; // This will be the object we return
      json = JSON.parse(json);
  
  
      // Extract the JSON we need 
      if(json[0].hasOwnProperty('dataValues')){
          console.log("HI: " + json[0].dataValues);
          returnedJson = json[0].dataValues; // This must be an INSERT...so dig deeper into the JSON object
      } else {
          console.log(json[0]);
          returnedJson = json[0]; // This is a find...so the JSON exists here
      }
  
      return returnedJson; // Finally return the json object so it can be used
  }
  </code></pre>
  
  <h3>Edit 4:</h3>
  
  <p>So there is an official sequelize method. Refer to the accepted answer below.</p>