# Newbie Question: Use Json file to show a list of names one by one

**URL:** https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837
**Category:** Coding Questions
**Created:** [November 30, 2020, 5:39pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837 "2020-11-30T17:39:31Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![denis](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@denis](https://discourse.processing.org/u/denis)
#### Post date: [November 30, 2020, 5:39pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/1 "2020-11-30T17:39:32Z")

</div>

Hi, I am trying to automate a list of names so that they appear to me one by one when I move my mouse. At first I did it with an array of images with the names inside and it worked. Now I am trying to convert it to a Json file to make it more convenient and automatic. I already managed to load the Json file but I don’t know how to ask it to show me the list one by one. Thank you!!

This was with the images:

```auto
 if (pmouseX != mouseX) {
 
       if(imgActual2 < imgCount2-1){
     imgActual2 ++;
           image(img01[imgActual2], 610,590,150,39);
     
      }else{
        imgActual2=0;
       }

```

but now i only was able to load de Json:

> JSONArray values;
> 
> void setup () {
> 
> values = loadJSONArray(“dataedr1.json”);
> 
> for (int j = 0; j \< values.size(); j++) {
> 
> ```
> JSONObject ig = values.getJSONObject(j); 
> 
> int id = ig.getInt("id");
> String name = ig.getString("name");
> 
> println(id + ", " + name);
> 
> ```
> 
> }  
> }

I only write the part of the code that is related to the Json file, I hope this is fine.

Thanks!

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [December 2, 2020, 12:15am UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/2 "2020-12-02T00:15:18Z")

</div>

Hi,

Why don’t you use the code that you used when you had the images for your JSON?

When you get the json object containing the id and the name at index `j` like this : `values.getJSONObject(j)`, you do the same but with the index that you increment when the mouse is moved. Then get the name.

Use the same technique for incrementing an index corresponding to the current name when the mouse is moved.

Note that you can also use the [`mouseMoved()`](https://processing.org/reference/mouseMoved_.html) function to detect when the mouse is moved rather than checking `mouseX` and `pmouseX`.

---

<div class="post-metadata">

### Author: ![denis](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@denis](https://discourse.processing.org/u/denis)
#### Post date: [December 3, 2020, 2:57pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/3 "2020-12-03T14:57:22Z")

</div>

Hey Josephh, thanks a lot for your help. Also, i didnt knew about the mouseMoved() so thanks also for that!

---

<div class="post-metadata">

### Author: ![denis](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@denis](https://discourse.processing.org/u/denis)
#### Post date: [December 3, 2020, 4:00pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/4 "2020-12-03T16:00:20Z")

</div>

Hey im sorry to bother again, but im trying to do it but still don’t completly understand how to:

This is what i’ve done:

> if(pmouseX != mouseX) {
> 
> ```
> if(int j = 0; j < values.size()) {
> j++;
>        
> text("name", 610,590,150,39);
>  
> }else{
> int j=0;
> }
> 
> ```
> 
> }

but i have this error: unexpected token: int

(i will change later the pmouseX and mouseX for the mouseMoved() but it made me an error bc of the if. I will figured out later:)

Thanks again!

---

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [December 3, 2020, 11:00pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/5 "2020-12-03T23:00:19Z")

</div>

Hello again,

You are messing things up here! 😁

The code sample that you posted should give you an error in the Processing IDE (in red btw). Also remember that you can auto format your code by pressing `Ctrl+T` to correct indentations and spaces.

It should help you to see where the error is. You put a semicolon inside the `if` but you can’t actually. You want to do an `AND`?

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 3, 2020, 11:02pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/6 "2020-12-03T23:02:15Z")

</div>

> [@denis](#):
>
> ```auto
> if(int j = 0; j < values.size()) {
> j++;
> 
> ```

This could also be a for loop but not correct

Or do you mean an if-clause?

Check out the reference

---

<div class="post-metadata">

### Author: ![denis](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@denis](https://discourse.processing.org/u/denis)
#### Post date: [December 3, 2020, 11:17pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/7 "2020-12-03T23:17:20Z")

</div>

this is so important! (Ctrl+T)

Thanks a lot, a didn’t knew that!

Im reading a lot this days but still very new with all,

I would do this now and see if i can see the error.

Thanks again for your time and your reply!

---

<div class="post-metadata">

### Author: ![denis](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@denis](https://discourse.processing.org/u/denis)
#### Post date: [December 3, 2020, 11:20pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/8 "2020-12-03T23:20:30Z")

</div>

The reference was a video but i needed to change it a little bit and this was the result, i was using if because i use it before, without the json, so i thought it would be a similar case.

which part is not correct? im sorry im learning and im very new with all of this.

Thanks again!

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 4, 2020, 7:01pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/9 "2020-12-04T19:01:20Z")

</div>

> [@denis](#):
>
> which part is not correct?

I guess what you want is:

before setup()

`int j = 0`

And then in the code where you have this section now:

```auto
if( j < values.size() -1 ) {
j++;

JSONObject ig = values.getJSONObject(j); 

int id = ig.getInt("id");
String name = ig.getString("name");

println(id + ", " + name);

}

```

It’d be cool if you would post your entire Sketch or a new smaller version of it that shows your problem. At best the Sketch would be runnable for us.

And also show some example lines of your JSON file so we try your code.

Chrisir

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [December 4, 2020, 10:44pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/10 "2020-12-04T22:44:53Z")

</div>

here is an example

data file:

```auto
[
{
"id": 2613357,
"name": "Smidstrup",
"country": "DK",
"coord": {
"lon": 12.55787,
"lat": 55.865688
}
},
{
"id": 6460975,
"name": "Rastnik",
"country": "BG",
"coord": {
"lon": 25.283331,
"lat": 41.400002
}
},
{
"id": 727762,
"name": "Rastnik",
"country": "BG",
"coord": {
"lon": 25.283331,
"lat": 41.400002
}
},
{
"id": 596826,
"name": "Murava",
"country": "LT",
"coord": {
"lon": 23.966669,
"lat": 54.916672
}
}
]

```

Sketch:

```auto

JSONArray json;

String result="";

void setup() {
  size(864, 833);
  background(111); 

  loadData();
}

void draw() {
  //
  background(111); 
  text(result, 24, 24);
} 

void loadData() {
  json = loadJSONArray("jsondata1.txt"); // !! // see reference/loadJSONArray_.html

  println( json.size()); 

  for (int i = 0; i < json.size(); i++) {

    println(i);

    JSONObject json2=json.getJSONObject(i); 
    println( json2 );
    // println( "--------------------------");

    String city1=json2.getString("name"); 

    println("--->>>"
      +city1);
    result+=city1+"\n";

    println( "--------------------------");
  }
}
//

```

---

<div class="post-metadata">

### Author: ![denis](https://avatars.discourse-cdn.com/v4/letter/d/5e9695/32.png) [@denis](https://discourse.processing.org/u/denis)
#### Post date: [December 7, 2020, 12:03pm UTC](https://discourse.processing.org/t/newbie-question-use-json-file-to-show-a-list-of-names-one-by-one/25837/11 "2020-12-07T12:03:20Z")

</div>

thanks a lot Chrisir! i will clean my code because is a mess and show it here, also the same with the json!

Thanks again for all the help
