# How to read and get the count from rest api JSON response?

**URL:** <https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829>\
**Category:** Off topic\
**Tags:** documentation\
**Created:** [September 14, 2021, 1:49pm UTC](https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829 "2021-09-14T13:49:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 14, 2021, 1:49pm UTC](https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829/1 "2021-09-14T13:49:09Z")

</div>

Hi,  
I have a json array response like below:

```auto
[
	{
		name : john
		dept: Mech
		Rank : A
	}
	{
		name : Mecury
		dept: Science
		Rank : A+
	}
	{
		name : Sana
		dept: Bio
		Rank : B
	}
]

```

when i try to get the length of the array i couldnt able to do that, because i dont have any prefix on the response i guess.

Is there any way i can get the length from this json array?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 14, 2021, 2:24pm UTC](https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829/2 "2021-09-14T14:24:59Z")

</div>

A repro in the playground would be nice. It is not directly related to babylon but in general you could do response.length ?

---

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 14, 2021, 4:27pm UTC](https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829/3 "2021-09-14T16:27:00Z")

</div>

not its throwing an wrong data when i tested on this below code:

```auto
var xhr = new XMLHttpRequest();
    xhr.open("GET", "https://jsonplaceholder.typicode.com/posts", true);
    xhr.onload = function(){
        console.log(xhr.responseText);
        console.log(xhr.responseText.length);

    };
    xhr.send(); 

```

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 14, 2021, 4:33pm UTC](https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829/4 "2021-09-14T16:33:37Z")

</div>

You need to parse the text response in this case with JSON.parse

---

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 14, 2021, 4:47pm UTC](https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829/5 "2021-09-14T16:47:35Z")

</div>

```auto
var jparse = JSON.parse(responsetext);
    var leng = Object.keys(jparse).length;
    console.log(leng);
   

```

tried this code but getting a syntax error:  
 ![image](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/3X/8/9/898b06c678648b54ff0bcfce8ff956ea6a416b13.png)

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 14, 2021, 8:36pm UTC](https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829/6 "2021-09-14T20:36:13Z")

</div>

This works: [Babylon.js Playground](https://playground.babylonjs.com/#NT2MQP)

Please, focus your topics around Babylon next time 🙂

---

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 15, 2021, 5:01am UTC](https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829/7 "2021-09-15T05:01:05Z")

</div>

Thank you 🙂

---

<div class="post-metadata">

**Author:** ![Siva\_s](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/siva_s/32/17498_2.png) [@Siva\_s](https://forum.babylonjs.com/u/Siva_s)\
**Post date:** [September 15, 2021, 11:38am UTC](https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829/8 "2021-09-15T11:38:00Z")

</div>

Getting ah 400 Bad Request error when i try to send the json data to below formate:

```auto
[
  {
    "postId": 1,
    "id": 1,
    "name": "id labore ex et quam laborum",
    "email": "Eliseo@gardner.biz",
    "body": "laudantium enim quasi est quidem magnam voluptate ipsam eos\ntempora quo necessitatibus\ndolor quam autem quasi\nreiciendis et nam sapiente accusantium"
  },
  {
    "postId": 1,
    "id": 2,
    "name": "quo vero reiciendis velit similique earum",
    "email": "Jayne_Kuhic@sydney.com",
    "body": "est natus enim nihil est dolore omnis voluptatem numquam\net omnis occaecati quod ullam at\nvoluptatem error expedita pariatur\nnihil sint nostrum voluptatem reiciendis et"
  },
  {
    "postId": 1,
    "id": 3,
    "name": "odio adipisci rerum aut animi",
    "email": "Nikita@garfield.biz",
    "body": "quia molestiae reprehenderit quasi aspernatur\naut expedita occaecati aliquam eveniet laudantium\nomnis quibusdam delectus saepe quia accusamus maiores nam est\ncum et ducimus et vero voluptates excepturi deleniti ratione"
  },
  {
    "postId": 1,
    "id": 4,
    "name": "alias odio sit",
    "email": "Lew@alysha.tv",
    "body": "non et atque\noccaecati deserunt quas accusantium unde odit nobis qui voluptatem\nquia voluptas consequuntur itaque dolor\net qui rerum deleniti ut occaecati"
  },
  {
    "postId": 1,
    "id": 5,
    "name": "vero eaque aliquid doloribus et culpa",
    "email": "Hayden@althea.biz",
    "body": "harum non quasi et ratione\ntempore iure ex voluptates in ratione\nharum architecto fugit inventore cupiditate\nvoluptates magni quo et"
  }
]

```

Can you please tell me what i am missing here?

---

<div class="post-metadata">

**Author:** ![sebavan](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/sebavan/32/57_2.png) [@sebavan](https://forum.babylonjs.com/u/sebavan)\
**Post date:** [September 15, 2021, 2:49pm UTC](https://forum.babylonjs.com/t/how-to-read-and-get-the-count-from-rest-api-json-response/23829/9 "2021-09-15T14:49:09Z")

</div>

Your server is returning this so it means you are not sending the expected format, I will move the post to off topic.
