# How to read Object in ionic 2?

**URL:** <https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399>\
**Category:** ionic-v3\
**Created:** [November 7, 2016, 12:43pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399 "2016-11-07T12:43:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![mahmoudismail](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mahmoudismail/32/103268_2.png) [@mahmoudismail](https://forum.ionicframework.com/u/mahmoudismail)\
**Post date:** [November 7, 2016, 12:43pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/1 "2016-11-07T12:43:45Z")

</div>

I am trying to fetch a `object` in `ionic2` which has key value and i want to read the **key** and the **brand name** from it within `ionic2`.

```
{
    "status": "success",
    "products": {
        "Key1": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
        "Key2": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
        "Key3": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
    }
}
```

---

<div class="post-metadata">

**Author:** ![chrisbenseler](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/chrisbenseler/32/90788_2.png) [@chrisbenseler](https://forum.ionicframework.com/u/chrisbenseler)\
**Post date:** [November 7, 2016, 12:52pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/2 "2016-11-07T12:52:33Z")

</div>

I don’t know if I have understand what you need. Your problem is to fetch the data form a server (you can use the Http module) or to read/parse this data?

In the second case, it’s an javascript issue, not related to Ionic.  
You can do something like:

```
let products = data.products; //data is the json from the example
Object.keys(products).forEach(key => {
  console.log(key); //Key1, Key2, Key3
  console.log(products[key][0].brand) //brandname
});
```

---

<div class="post-metadata">

**Author:** ![mahmoudismail](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mahmoudismail/32/103268_2.png) [@mahmoudismail](https://forum.ionicframework.com/u/mahmoudismail)\
**Post date:** [November 7, 2016, 12:56pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/3 "2016-11-07T12:56:19Z")

</div>

@chrisbenseler Exactly in the second case, i want to read the **key** and the **brand name** dynamically so how i can do this ? thank you.

---

<div class="post-metadata">

**Author:** ![Ellezo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ellezo/32/104261_2.png) [@Ellezo](https://forum.ionicframework.com/u/Ellezo)\
**Post date:** [November 7, 2016, 1:33pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/4 "2016-11-07T13:33:16Z")

</div>

Let’s say for example, this value is from a json file or from the server and this comes from a variable name “result”

you can then read the key and brand name through

> [@](#):
>
> let products: any = result[“products”];  
> let keyName: string;  
> let brandName: string;
> 
> for (let key in products) {  
> keyName = key;
> 
> for (let item of products[key]) {  
> brandName = item[“name”];  
> }  
> }

---

<div class="post-metadata">

**Author:** ![mahmoudismail](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mahmoudismail/32/103268_2.png) [@mahmoudismail](https://forum.ionicframework.com/u/mahmoudismail)\
**Post date:** [November 7, 2016, 1:51pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/5 "2016-11-07T13:51:38Z")

</div>

@Ellezo i’ve got `brandName-> undefined` .

---

<div class="post-metadata">

**Author:** ![Ellezo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ellezo/32/104261_2.png) [@Ellezo](https://forum.ionicframework.com/u/Ellezo)\
**Post date:** [November 7, 2016, 2:08pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/6 "2016-11-07T14:08:08Z")

</div>

Updated my answer. Kindly check it.

---

<div class="post-metadata">

**Author:** ![mahmoudismail](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mahmoudismail/32/103268_2.png) [@mahmoudismail](https://forum.ionicframework.com/u/mahmoudismail)\
**Post date:** [November 7, 2016, 2:16pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/7 "2016-11-07T14:16:22Z")

</div>

ok thank you it’s working but another question in a new case

```
{
    "status": "success",
    "products": {
        "Key1": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }, {
              "entity_id": "4438",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
        "Key2": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            },
 {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
        "Key3": [
            {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            },
 {
              "entity_id": "448",
              "sku": "587",
              "name": "name",
              "image_url": "587.png",
              "price": "15,000",
              "qty": 0,
              "rating": 0,
              "wishlist": false,
              "specialprice": "7,500",
              "brand": "brandname"
            }
        ],
    }
}
```

---

<div class="post-metadata">

**Author:** ![Ellezo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ellezo/32/104261_2.png) [@Ellezo](https://forum.ionicframework.com/u/Ellezo)\
**Post date:** [November 7, 2016, 2:28pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/8 "2016-11-07T14:28:50Z")

</div>

So what’s the question? Same code can be applied in the new result data.

---

<div class="post-metadata">

**Author:** ![mahmoudismail](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mahmoudismail/32/103268_2.png) [@mahmoudismail](https://forum.ionicframework.com/u/mahmoudismail)\
**Post date:** [November 7, 2016, 2:29pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/9 "2016-11-07T14:29:36Z")

</div>

i want to get some data but in the new result data like above , thank you in advanced.

---

<div class="post-metadata">

**Author:** ![Ellezo](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/ellezo/32/104261_2.png) [@Ellezo](https://forum.ionicframework.com/u/Ellezo)\
**Post date:** [November 7, 2016, 2:38pm UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/10 "2016-11-07T14:38:53Z")

</div>

> [@](#):
>
> let products: any = result[“products”];  
> let keyName: string;  
> let brandName: string;  
> for (let key in products) {
> 
> for (let item of products[key]) {  
> keyName = key;  
> brandName = item[“name”];  
> }  
> }

---

<div class="post-metadata">

**Author:** ![mahmoudismail](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mahmoudismail/32/103268_2.png) [@mahmoudismail](https://forum.ionicframework.com/u/mahmoudismail)\
**Post date:** [November 8, 2016, 10:48am UTC](https://forum.ionicframework.com/t/how-to-read-object-in-ionic-2/69399/11 "2016-11-08T10:48:56Z")

</div>

To print `"Key"` array in `*ngFor` loop, first write one `Pipes`

**Pipe.ts**

```
import { Component,Pipe, PipeTransform, Injectable } from '@angular/core';

@Pipe({
  name: 'objectValues'
})

@Injectable()
export class ObjectValuesPipe implements PipeTransform {
  transform(obj: any) {
    let result = [];
    for (var key in obj) {
      if (obj.hasOwnProperty(key)) {
        result.push(obj[key]);
      }
    }
    return result;
  }
}

```

do not forget to import your `Pipes` in `@NgModule`, than you can use this pipe like this.

```
<ul*ngFor="let item of items">
   <li*ngFor="let value of item | objectValues">
     {{ value }}
   </li>
 </ul>
```
