# Creating serializer dynamically

**URL:** <https://discuss.emberjs.com/t/creating-serializer-dynamically/12599>\
**Category:** Ember Data\
**Created:** [March 15, 2017, 3:12pm UTC](https://discuss.emberjs.com/t/creating-serializer-dynamically/12599 "2017-03-15T15:12:28Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zardaloop](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/zardaloop/32/12972_2.png) [@zardaloop](https://discuss.emberjs.com/u/zardaloop)\
**Post date:** [March 15, 2017, 3:12pm UTC](https://discuss.emberjs.com/t/creating-serializer-dynamically/12599/1 "2017-03-15T15:12:28Z")

</div>

Here is the issue I have when I call ds.store.query my server returns data like this :

{ “Job”: [{ “Customer\_Name”: { “Primary\_Key”: “Customer1”, “Contact”: “Contact 1” } }] }

but if I call it using for examle ds.findAll it returns like this :

{ “Job”: [{ “Customer\_Name”: “Customer1” }] }

so it means the Customer\_Name which here is the foreign key or in ember world belong-to type to the customer table does not get resolved.

So I am trying to define a pattern to deal with both scenario in the app//serializer/application.js file using propabbly normalizeQueryResponse to serialize data when ds.store.query is called. Because I can’t define a serializer for Job model when there is two different scenarios for serializing the data, can I ?

Could someone please advise what should I do ?

this is what I have so far but it doesnt work :

normalizeQueryResponse(store, primaryModelClass, payload, id, requestType) { console.log(payload);

```
function getListOfkeys(payload) {
  var list = [];
  payload.forEach(function (item) {
    for (var key in item) {
      if (item.hasOwnProperty(key)) {
        if (item[key] !== null && typeof item[key] === 'object') {
          list.pushObject(key);
        }
      }
    }
  }, this);
  return list;
}
var keys = getListOfkeys(payload[Ember.String.capitalize(primaryModelClass.modelName)]);

if (keys.length > 0) {
  let attrs = {};
  keys.forEach(function (key) {
    attrs[key] = {
      serialize: 'records',
      deserialize: 'records'
    }
  }, this);
  Ember.getOwner(store).registry.register('serializer:' + Ember.String.dasherize(primaryModelClass.modelName), DS.RESTSerializer.extend(DS.EmbeddedRecordsMixin, { attrs: attrs }));
}

var normaliszedResponse = this._normalizeResponse(store, primaryModelClass, payload, id, requestType, true);
return normaliszedResponse;

```

},

Many thanks in advance
