# Ember data 1.0 beta extractArray Serializer

**URL:** <https://discuss.emberjs.com/t/ember-data-1-0-beta-extractarray-serializer/2444>\
**Category:** Ember Data\
**Created:** [September 5, 2013, 6:35am UTC](https://discuss.emberjs.com/t/ember-data-1-0-beta-extractarray-serializer/2444 "2013-09-05T06:35:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![abuder](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/abuder/32/14983_2.png) [@abuder](https://discuss.emberjs.com/u/abuder)\
**Post date:** [September 5, 2013, 6:35am UTC](https://discuss.emberjs.com/t/ember-data-1-0-beta-extractarray-serializer/2444/1 "2013-09-05T06:35:20Z")

</div>

What is the best way to serialize these objects? I have an array of posts, these objects include embedded objects of user.

```
 {
"posts": [
  {
   "id": "5226f2670364e70ae7d77266",
   "subject": "Text",
   "created": 1325410935048,
   "reporter": {
           "id": "5226f2660364e70ae7d771e2",
           "firstName": "Doris",
           "lastName": "Baumertr"
   },
  {
   "id": "5226f2670364e75644156464",
   "subject": "Post 2",
   "created": 1325410935048,
   "reporter": {
           "id": "5226f2660364e70ae7d771e2",
           "firstName": "Doris",
           "lastName": "Baumertr"
   }
 ]

```

}

I defined the serializer of the post object, what I should do to map embedded objects?

```
App.PostSerializer = DS.RESTSerializer.extend({
    extractArray: function(store, type, payload, id, requestType) {

        return this._super(store, type, payload, id, requestType);
    }
});

```

---

<div class="post-metadata">

**Author:** ![abuder](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/abuder/32/14983_2.png) [@abuder](https://discuss.emberjs.com/u/abuder)\
**Post date:** [September 5, 2013, 6:56am UTC](https://discuss.emberjs.com/t/ember-data-1-0-beta-extractarray-serializer/2444/2 "2013-09-05T06:56:44Z")

</div>

I think I solved the problem with the following code.

```
App.PostSerializer = DS.RESTSerializer.extend({
    extractArray: function(store, type, payload, id, requestType) {
        var posts =payload.posts;
        var reporters = [];
        posts.forEach(function(post){
               var reporter = post.reporter,
                   reporterId = reporter.id;
                reporters.push(reporter);
                post.reporter = reporterId;
        });
        payload.users = reporters;

        return this._super(store, type, payload, id, requestType);
    }
});

```

After extracting the json structure looks so:

```
{
"posts": [
  {
   "id": "5226f2670364e70ae7d77266",
   "subject": "Text",
   "created": 1325410935048,
   "reporter": "5226f2660364e70ae7d771e2"
   },
  {
   "id": "5226f2670364e75644156464",
   "subject": "Post 2",
   "created": 1325410935048,
   "reporter": "5226f2660364e70ae7d771e2"
 }
 ],
 "users": [
    "reporter": {
           "id": "5226f2660364e70ae7d771e2",
           "firstName": "Doris",
           "lastName": "Baumertr"
   },
    "reporter": {
           "id": "5226f2660364e70ae7d771e2",
           "firstName": "Doris",
           "lastName": "Baumertr"
   }
  ]
}

```

---

<div class="post-metadata">

**Author:** ![samuraisam](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/samuraisam/32/226_2.png) [@samuraisam](https://discuss.emberjs.com/u/samuraisam)\
**Post date:** [December 27, 2013, 1:06am UTC](https://discuss.emberjs.com/t/ember-data-1-0-beta-extractarray-serializer/2444/3 "2013-12-27T01:06:39Z")

</div>

This doesn’t seem to work for me because the embedded resources are not saved to the store. So when you try to access the embedded object, an exception is thrown that the object does not exist in the store. I’m assuming this is because `extractArray` does not expect an arbitrary property to be set on the returned array (and it pukes if you return anything besides an array).

Is there an updated way to accomplish this? Are you supposed to tell it to insert those objects into the store explicitly?

---

<div class="post-metadata">

**Author:** ![samuraisam](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/samuraisam/32/226_2.png) [@samuraisam](https://discuss.emberjs.com/u/samuraisam)\
**Post date:** [December 27, 2013, 1:15am UTC](https://discuss.emberjs.com/t/ember-data-1-0-beta-extractarray-serializer/2444/4 "2013-12-27T01:15:47Z")

</div>

Here is a GIST with the original `extract*` method I wrote:

[https://gist.github.com/samuraisam/8140992](https://gist.github.com/samuraisam/8140992)

---

<div class="post-metadata">

**Author:** ![samuraisam](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.emberjs.com/samuraisam/32/226_2.png) [@samuraisam](https://discuss.emberjs.com/u/samuraisam)\
**Post date:** [December 27, 2013, 1:15am UTC](https://discuss.emberjs.com/t/ember-data-1-0-beta-extractarray-serializer/2444/5 "2013-12-27T01:15:55Z")

</div>

And this one which actually works, but I’m unsure if it’s safe to be inserting into the store here, and not sure if it takes advantage of any of those other (potentially necessary) transformations).

[https://gist.github.com/samuraisam/8141094](https://gist.github.com/samuraisam/8141094)
