# JSON - generating a random value from a certain section with mousePressed()

**URL:** <https://discourse.processing.org/t/json-generating-a-random-value-from-a-certain-section-with-mousepressed/7558>\
**Category:** Coding Questions\
**Tags:** homework\
**Created:** [January 16, 2019, 11:35am UTC](https://discourse.processing.org/t/json-generating-a-random-value-from-a-certain-section-with-mousepressed/7558 "2019-01-16T11:35:46Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![anon98404100](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@anon98404100](https://discourse.processing.org/u/anon98404100)\
**Post date:** [January 16, 2019, 11:35am UTC](https://discourse.processing.org/t/json-generating-a-random-value-from-a-certain-section-with-mousepressed/7558/1 "2019-01-16T11:35:46Z")

</div>

Working with JSON files - I have to recall a certain value with mousePressed() in a specific category.

In my code you have the following code :

```auto
var chuckData;

function setup () {
    createCanvas (displayWidth, 500);
    loadJSON("https://api.icndb.com/jokes/random", getData);
    
}

function draw () {
background(220);
    
    if (chuckData) {
            text(chuckData.value.joke, 10, 200);  
        }
    }

function getData(data) {
    chuckData = data;
}

function mousePressed () {
    text(chuckData.value.joke, 100, 200);
    
}

```

In this code I load the link --\> [https://api.icndb.com/jokes/random](https://api.icndb.com/jokes/random) (clicking the link will highlikely show 1 joke. but changes on refresh)

There is a section called “joke” I need to retrieve a joke from, and then generate it into text on screen.

This being :

```auto
if (chuckData) {
            text(chuckData.value.joke, 10, 200);  
        }
    }

```

I managed to get the text to print on the screen, and it changes the joke on refresh, however: I need to get this effect when using mousePressed() and fail to find out how to do it or find it on the internet.

Any help is appreciated!

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 16, 2019, 11:42am UTC](https://discourse.processing.org/t/json-generating-a-random-value-from-a-certain-section-with-mousepressed/7558/2 "2019-01-16T11:42:06Z")

</div>

with mouse pressed ( released ) you set something, not draw anything…  
but that setting can use in draw.

```auto
var chuckData;
var thejoke="test: click";

function setup () {
  createCanvas (displayWidth, 500);
  loadJSON("https://api.icndb.com/jokes/random", getData);
}

function draw () {
  background(220);

  if (chuckData) {
    text(thejoke, 10, 200);
  }
}

function getData(data) {
  chuckData = data;
}

function mouseReleased () {
  thejoke = chuckData.value.joke;
}

```

---

<div class="post-metadata">

**Author:** ![anon98404100](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@anon98404100](https://discourse.processing.org/u/anon98404100)\
**Post date:** [January 16, 2019, 11:47am UTC](https://discourse.processing.org/t/json-generating-a-random-value-from-a-certain-section-with-mousepressed/7558/3 "2019-01-16T11:47:39Z")

</div>

@kll but how can I get a random joke from the file when continuously clicking?

As I showed in my code: When starting the page, a joke shows.

Continuously clicking should change the joke. Now I am still forced to refresh the page, but now need to click and release to show the joke (when using your example).

how should I tackle that?

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [January 16, 2019, 11:51am UTC](https://discourse.processing.org/t/json-generating-a-random-value-from-a-certain-section-with-mousepressed/7558/4 "2019-01-16T11:51:02Z")

</div>

must load again i think:

```auto
function mouseReleased () {
  loadJSON("https://api.icndb.com/jokes/random", getData);
  thejoke = chuckData.value.joke;
}

```

---

<div class="post-metadata">

**Author:** ![anon98404100](https://avatars.discourse-cdn.com/v4/letter/a/b3f665/32.png) [@anon98404100](https://discourse.processing.org/u/anon98404100)\
**Post date:** [January 16, 2019, 11:53am UTC](https://discourse.processing.org/t/json-generating-a-random-value-from-a-certain-section-with-mousepressed/7558/5 "2019-01-16T11:53:53Z")

</div>

I got it, thanks!

What you mentioned about re-loading the JSON in the function seems to work.

I changed it back to

```auto
function mousePressed() {

loadJSON("https://api.icndb.com/jokes/random", getData);

}

```

and that seemed to work. Thank you 🙂
