# Trying to load an array from a JSON

**URL:** <https://discourse.processing.org/t/trying-to-load-an-array-from-a-json/685>\
**Category:** Coding Questions\
**Created:** [June 3, 2018, 7:46pm UTC](https://discourse.processing.org/t/trying-to-load-an-array-from-a-json/685 "2018-06-03T19:46:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Red\_Hen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/red_hen/32/323_2.png) [@Red\_Hen](https://discourse.processing.org/u/Red_Hen)\
**Post date:** [June 3, 2018, 7:46pm UTC](https://discourse.processing.org/t/trying-to-load-an-array-from-a-json/685/1 "2018-06-03T19:46:43Z")

</div>

Hello!

So, I’m trying to get data from a JSON file, where the data was saved as an array. But when I try to retrieve discrete values, they come back as ‘undefined’.

Can you help? What am I doing wrong?

```auto
let locJ = 'designs.json';
    jL = loadJSON(locJ);
    
  
    console.log(jL.boxs[0].e);

```

**Console = Cannot read property ‘0’ of undefined**

This is what the successfully(?) saved JSON file looks like (generated from p5 code):

```auto
{
  "boxs": [
    {
      "x": 73.1,
      "y": 73.1,
      "r": 34.4,
      "gX": 1,
      "gY": 1,
      "h": false,
      "p": false,
      "e": false
    },
    {
      "x": 73.1,
      "y": 107.5,
      "r": 34.4,
      "gX": 1,
      "gY": 2,
      "h": false,
      "p": false,
      "e": false
    },
    {
      "x": 73.1,
      "y": 141.89999999999998,
      "r": 34.4,
      "gX": 1,
      "gY": 3,
      "h": false,
      "p": false,
      "e": false
    },

**etc**

```

Thanks for any help 🙂

B

---

<div class="post-metadata">

**Author:** ![Red\_Hen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/red_hen/32/323_2.png) [@Red\_Hen](https://discourse.processing.org/u/Red_Hen)\
**Post date:** [June 3, 2018, 8:13pm UTC](https://discourse.processing.org/t/trying-to-load-an-array-from-a-json/685/2 "2018-06-03T20:13:56Z")

</div>

…And solved!

My code wasn’t giving the JSON file long enough to load 😅

So, have made sure that the file is successfully loaded in the **preload()** function.

Shiffman’s video is great on this topic: [Giant Eagles](https://www.youtube.com/watch?v=118sDpLOClw)

B x

---

<div class="post-metadata">

**Author:** ![bmoren](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bmoren/32/68_2.png) [@bmoren](https://discourse.processing.org/u/bmoren)\
**Post date:** [June 9, 2018, 12:16am UTC](https://discourse.processing.org/t/trying-to-load-an-array-from-a-json/685/3 "2018-06-09T00:16:05Z")

</div>

nice one! In the future if you dont want to preload, you could use a callback to call a function once the data is ready to be processed there is an example in the p5 reference 1/2 way down here: [https://p5js.org/reference/#/p5/loadJSON](https://p5js.org/reference/#/p5/loadJSON)

Here’s an example using an arrow function to offer another direction from the p5js documentation  
( ⚠ pseudo-code ahead)

```javascript
  let locJ = 'designs.json';
  jL = loadJSON(locJ, (incomingData)=>{
    //code to execute once the incoming data has fully loaded, this is a callback
    console.log(jL.boxs[0].e);
  });

```

---

<div class="post-metadata">

**Author:** ![Red\_Hen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/red_hen/32/323_2.png) [@Red\_Hen](https://discourse.processing.org/u/Red_Hen)\
**Post date:** [June 9, 2018, 10:21am UTC](https://discourse.processing.org/t/trying-to-load-an-array-from-a-json/685/4 "2018-06-09T10:21:36Z")

</div>

Brilliant, thank you!
