# Examples from "Make: Getting Started with p5.js" not working

**URL:** <https://discourse.processing.org/t/examples-from-make-getting-started-with-p5-js-not-working/37835>\
**Category:** Beginners\
**Created:** [July 4, 2022, 6:34pm UTC](https://discourse.processing.org/t/examples-from-make-getting-started-with-p5-js-not-working/37835 "2022-07-04T18:34:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![MissAda](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@MissAda](https://discourse.processing.org/u/MissAda)\
**Post date:** [July 4, 2022, 6:34pm UTC](https://discourse.processing.org/t/examples-from-make-getting-started-with-p5-js-not-working/37835/1 "2022-07-04T18:34:52Z")

</div>

Hi hi, all! I’ve been working with p5 for a few months now. Going through the “getting started with p5.js” book from McCarthy + Reas. I’ve noticed that even if I copy the examples exactly as written, they sometimes don’t work. Case in point → [p5.js Web Editor](https://editor.p5js.org/MissAda/sketches/A7O8cmu5S)

this is a direct copy of what’s written on pg 147 of said book, a book whose .pdf version may be found via a brief google.

It’s super frustrating for someone that is trying to learn. Are these examples outdated or am I doing something wrong?? Little help?

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 4, 2022, 6:46pm UTC](https://discourse.processing.org/t/examples-from-make-getting-started-with-p5-js-not-working/37835/2 "2022-07-04T18:46:54Z")

</div>

> [@MissAda](#):
>
> Are these examples outdated or am I doing something wrong??

- Neither!! File “films.json” is an array of objects.
- However **loadJSON()** is returning an object instead of an array!
- So _filmData.length_ is zero! Even though it indeed contains 12 objects.
- According to **loadJSON()**'s online reference: [reference | p5.js](http://p5js.org/reference/#/p5/loadJSON)
- it’s supposed to return either an object or an array.
- So it’s a bug which was introduced later.
- You should open an issue on its repo:

> **[Issues · processing/p5.js](https://github.com/processing/p5.js/issues)**
>
> p5.js is a client-side JS platform that empowers artists, designers, students, and anyone to learn to code and express themselves creatively on the web. It is based on the core principles of Proces...

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [July 4, 2022, 7:21pm UTC](https://discourse.processing.org/t/examples-from-make-getting-started-with-p5-js-not-working/37835/3 "2022-07-04T19:21:30Z")

</div>

As a temporary workaround you can use Object.**values()** in order to convert the object returned by [**loadJSON()**](https://p5js.org/reference/#/p5/loadJSON) to a proper array:

> **[Object.values() - JavaScript | MDN](https://developer.Mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values)**
>
> The Object.values() method
> returns an array of a given object's own enumerable property values, in the same order
> as that provided by a for...in loop. (The only
> difference is that a for...in loop enumerates properties in the prototype
> chain...

```auto
var filmData;

function preload() {
  loadJSON("films.json", json => filmData = Object.values(json));
}

```

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 4, 2022, 8:52pm UTC](https://discourse.processing.org/t/examples-from-make-getting-started-with-p5-js-not-working/37835/4 "2022-07-04T20:52:10Z")

</div>

Hello @MissAda ,

This worked:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/3/130f511beb7987aaf541354c2207850db21a9107.png)

References:

> <https://stackoverflow.com/questions/21552402/javascript-array-length-returns-undefined>

> <https://stackoverflow.com/questions/5223/length-of-a-javascript-object>

`:)`

---

<div class="post-metadata">

**Author:** ![MissAda](https://avatars.discourse-cdn.com/v4/letter/m/9e8a1a/32.png) [@MissAda](https://discourse.processing.org/u/MissAda)\
**Post date:** [July 5, 2022, 3:21pm UTC](https://discourse.processing.org/t/examples-from-make-getting-started-with-p5-js-not-working/37835/5 "2022-07-05T15:21:03Z")

</div>

Thanks to both GLV and GoToLoop! Your solutions worked and are helping me understanding how the example was supposed to function, as well as looking at alternative ways to approach it. Your help matters - thank you!
