# How 2 count number of elements in json array using string(json

**URL:** https://discourse.cmake.org/t/how-2-count-number-of-elements-in-json-array-using-string-json/2709
**Category:** Usage
**Created:** [February 6, 2021, 6:06pm UTC](https://discourse.cmake.org/t/how-2-count-number-of-elements-in-json-array-using-string-json/2709 "2021-02-06T18:06:35Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![leeruns](https://discourse.cmake.org/user_avatar/discourse.cmake.org/leeruns/32/799_2.png) [@leeruns](https://discourse.cmake.org/u/leeruns)
#### Post date: [February 6, 2021, 6:06pm UTC](https://discourse.cmake.org/t/how-2-count-number-of-elements-in-json-array-using-string-json/2709/1 "2021-02-06T18:06:35Z")

</div>

Simple usage question here. If I want to count the size of a json array

- read json file
- count number of entryies in top level array
- save number of entries as a variable  
`file(read JSON input.json input)`  
`string(JSON len LENGTH ${input} 0 ARRAY_MEMBERS) `  
`set( length_of_array ${len})`

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [February 6, 2021, 6:26pm UTC](https://discourse.cmake.org/t/how-2-count-number-of-elements-in-json-array-using-string-json/2709/2 "2021-02-06T18:26:24Z")

</div>

I think this would work better:

```cmake
file(READ "input.json" input)
string(JSON length_of_array LENGTH "${input}" "")

```

though I’m not 100% sure on how to reference the top-level object/array (that last empty string argument).
