# How get variables from Json Objects

**URL:** <https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401>\
**Category:** ionic-v3\
**Created:** [September 22, 2017, 12:51pm UTC](https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401 "2017-09-22T12:51:13Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhilAichinger](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@PhilAichinger](https://forum.ionicframework.com/u/PhilAichinger)\
**Post date:** [September 22, 2017, 12:51pm UTC](https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401/1 "2017-09-22T12:51:13Z")

</div>

i have an api as the following:

```auto
 getUserTanks(userID: string): Observable<Tank[]> {
    return this.http.get(`${this.ApiUrl}/tank/read_user_tanks.php?userID=${userID}`)
      .map(res => <Tank[]>res.json());
     
  }

```

i am trying to return these parameters from the API:  
Height, Length and Width which are Keys that hold values in the JSON Object see below:  
the API is working and im able to return values and bind them like so {{tank.Height}} in the HTML but i need to run functions and calculations based on the returned values.

`[{"ID":"27","Name":"PhilsTank","Width":"12.0000","Length":null,"Height":"12.0000","Volume":"0.0000","GlassThickness":"12.0000","TypeID":"1","Sump":"12","UserID":"1","Active":"1"},{"ID":"32","Name":"Lousias tank","Width":"123456.0000","Length":1000.0000,"Height":"234567.0000","Volume":"0.0000","GlassThickness":"12342.0000","TypeID":"1","Sump":"123","UserID":"1","Active":"1"}]`

I want to acheive something like the code below excuse syntax as this is on the fly:

> CalcVolume(){
> 
> let Tankheight = this.tanks.Height;  
> let TankWidth = this.tanks.Width;  
> Let TankLength = this.tanks.Length;
> 
> let tankVolume = this.TankHeight_this.TankLength_this.TankWidth/100000;
> 
> console.log(tankVolume,“litres”)  
> }

how do i access the Keys and retrieve their values?

---

<div class="post-metadata">

**Author:** ![FnnHuman](https://avatars.discourse-cdn.com/v4/letter/f/5f8ce5/32.png) [@FnnHuman](https://forum.ionicframework.com/u/FnnHuman)\
**Post date:** [September 22, 2017, 1:24pm UTC](https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401/2 "2017-09-22T13:24:30Z")

</div>

```auto
.map(res => res.json())
.subscribe(data => {
console.log(data[0].ID);
});

```

---

<div class="post-metadata">

**Author:** ![PhilAichinger](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@PhilAichinger](https://forum.ionicframework.com/u/PhilAichinger)\
**Post date:** [September 22, 2017, 1:39pm UTC](https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401/3 "2017-09-22T13:39:31Z")

</div>

i forgot to add this code that ive already got…

tanks = Tank []  
this.userID = “1”;

```
      apiProvider.getUserTanks(this.userID).subscribe(tanks => {
        this.tanks = tanks; })
```

---

<div class="post-metadata">

**Author:** ![PhilAichinger](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@PhilAichinger](https://forum.ionicframework.com/u/PhilAichinger)\
**Post date:** [September 22, 2017, 2:55pm UTC](https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401/4 "2017-09-22T14:55:49Z")

</div>

how do i add that to the existing code? rather than just pulling say Tank0 statically i would like to pull it dynamically as i have multiple arrays in the JSON file.?

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [September 22, 2017, 9:54pm UTC](https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401/5 "2017-09-22T21:54:38Z")

</div>

I would do this as a postprocessing stage in the provider, so by the time `getUserTanks()` returns, it has `volume` properties in all the tanks:

```auto
static fillOutTank(tank: Tank): Tank {
  tank.volume = tank.height * tank.length * tank.width;
  return tank;
}

getUserTanks(uid: string): Observable<Tank[]> {
  return whateverYouWereAlreadyDoing(uid).map(tanks => tanks.map(TankProvider.fillOutTank));
}

```

---

<div class="post-metadata">

**Author:** ![PhilAichinger](https://avatars.discourse-cdn.com/v4/letter/p/f4b2a3/32.png) [@PhilAichinger](https://forum.ionicframework.com/u/PhilAichinger)\
**Post date:** [September 24, 2017, 8:25am UTC](https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401/6 "2017-09-24T08:25:17Z")

</div>

hi rapropos could you explain this in a bit more detail as i have tried your method but i get an issue with the .map function of tanks.map saying .map could not be found in response.

if you could help me on this that would be really grateful.

---

<div class="post-metadata">

**Author:** ![Tommertom](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Tommertom](https://forum.ionicframework.com/u/Tommertom)\
**Post date:** [September 24, 2017, 10:48am UTC](https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401/7 "2017-09-24T10:48:06Z")

</div>

Hi

I dont see you iterate through the array of tanks in order to access individual tank properties. Hence tanks.height dont make no sense to me as you ask a property of an array like this,not an indivudual tank

This.tanks.map((tank)=\>{ do stuff with tank })

Regards

Tom

---

<div class="post-metadata">

**Author:** ![Tommertom](https://avatars.discourse-cdn.com/v4/letter/t/ed8c4c/32.png) [@Tommertom](https://forum.ionicframework.com/u/Tommertom)\
**Post date:** [September 24, 2017, 11:23am UTC](https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401/8 "2017-09-24T11:23:10Z")

</div>

Btw, and if you really want to play cool, iterate through the array via RxJS as you are receiving your stuff through Observable stream.

---

<div class="post-metadata">

**Author:** ![rapropos](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/rapropos/32/100227_2.png) [@rapropos](https://forum.ionicframework.com/u/rapropos)\
**Post date:** [September 24, 2017, 9:07pm UTC](https://forum.ionicframework.com/t/how-get-variables-from-json-objects/106401/9 "2017-09-24T21:07:44Z")

</div>

> [@PhilAichinger](#):
>
> i get an issue with the .map function of tanks.map saying .map could not be found in response.

`whateverYouWereAlreadyDoing()` is supposed to return an `Observable<Tank[]>`, so something like:

```auto
this.http.get(url).map(rsp => rsp.json())

```
