# Accessing Json object

**URL:** <https://discourse.processing.org/t/accessing-json-object/10659>\
**Category:** Coding Questions\
**Created:** [April 26, 2019, 11:14am UTC](https://discourse.processing.org/t/accessing-json-object/10659 "2019-04-26T11:14:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Honkitonkie](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@Honkitonkie](https://discourse.processing.org/u/Honkitonkie)\
**Post date:** [April 26, 2019, 11:14am UTC](https://discourse.processing.org/t/accessing-json-object/10659/1 "2019-04-26T11:14:40Z")

</div>

Hi all,

I recently started a project where i want to do some aninmations with weather data. However i got stuck rather quickly and can’t seem to acces my JSON.

I looked everywhere and tried a lot but the answer still eludes me.  
After yet another hour of wondering stackoverflow, MDN and various other resources I set my hopes on your vast knowledge.

Topic:

I have JSON data formatted like so:

```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
    }
  }
]

```

I load it in like so and it appears as an object:

```auto
cities = loadJSON('city.list.test.json');
console.log(cities);

```

When I try to access a single city it tells me the value is undefined or that I use disallowed characters

From here on I tried numerous things and thought of scandalous means to get access (like treating it the objects as an array and loading in my object in an array (with the vague hope of it being usefull)), Also i tried various object properties like object.keys and object.values but didn’t get me where I needed to be.

Although it seems likely to me that I need to access the object within the object I have run out of ideas how to go about doing so.

A push in the right direction would be much appreciated.

---

<div class="post-metadata">

**Author:** ![hotfooted](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/hotfooted/32/3670_2.png) [@hotfooted](https://discourse.processing.org/u/hotfooted)\
**Post date:** [April 26, 2019, 12:14pm UTC](https://discourse.processing.org/t/accessing-json-object/10659/2 "2019-04-26T12:14:20Z")

</div>

actually what about [this](https://editor.p5js.org/hotfooted/sketches/JWc6OP21K)

you can read why this gives access over [here](https://p5js.org/reference/#/p5/loadJSON)

but i ain’t no json guy so this is just me having a go

---

<div class="post-metadata">

**Author:** ![Honkitonkie](https://avatars.discourse-cdn.com/v4/letter/h/dec6dc/32.png) [@Honkitonkie](https://discourse.processing.org/u/Honkitonkie)\
**Post date:** [April 26, 2019, 1:16pm UTC](https://discourse.processing.org/t/accessing-json-object/10659/3 "2019-04-26T13:16:33Z")

</div>

Amazing. Thanks a bunch.  
I can’t believe i overlooked that the JSON should be called with a callback here.

Thank you for the help and linking the reference

---

<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:** [April 26, 2019, 1:22pm UTC](https://discourse.processing.org/t/accessing-json-object/10659/4 "2019-04-26T13:22:28Z")

</div>

example **[in Java mode]**

```auto
JSONArray json;

// City[] cities; // cities

String result="";

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

  // cities = new City [5];
  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);
    // cities[i] = new City(); 

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

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

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

    println( "--------------------------");

    //cities[i].id = json2.getInt("id");
    //cities[i].name = json2.getString("name");
    //cities[i].value = json2.getString("value");
    //println(cities[i].label);
  }
}

// =====================================================

class City {
  int id;
  String name;
  String value;
}
//

```

---

<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:** [April 26, 2019, 1:40pm UTC](https://discourse.processing.org/t/accessing-json-object/10659/5 "2019-04-26T13:40:44Z")

</div>

> **[JSON Religions](https://bl.ocks.org/GoSubRoutine/9aa9cd00000e331612c8e557b5dd28f0)**
>
> GoSubRoutine’s Block 9aa9cd00000e331612c8e557b5dd28f0

  

> **[JSON Open-Notify ISS](http://bl.ocks.org/GoSubRoutine/7a567f0510b338b6d0fc1ef53f24f10e)**
>
> GoSubRoutine’s Block 7a567f0510b338b6d0fc1ef53f24f10e

  

> **[OpenWeatherMapJSON](http://bl.ocks.org/GoSubRoutine/5fbc03e019c53254a2ba7e7fd3318b45)**
>
> GoSubRoutine’s Block 5fbc03e019c53254a2ba7e7fd3318b45
