# Get elements in JSONObject

**URL:** <https://discourse.processing.org/t/get-elements-in-jsonobject/41048>\
**Category:** Coding Questions\
**Created:** [February 24, 2023, 11:30am UTC](https://discourse.processing.org/t/get-elements-in-jsonobject/41048 "2023-02-24T11:30:30Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)\
**Post date:** [February 24, 2023, 11:30am UTC](https://discourse.processing.org/t/get-elements-in-jsonobject/41048/1 "2023-02-24T11:30:30Z")

</div>

I try to go in `JSONObject` but I don’t find a solution to go inside, except if I use knew `key`. But my purpose is going to the object to grad all informations from it, I don’t know the information in advance.  
I see the `JSONObject` is an `HashMap`, but when I try to apply the function from `HashMap` like `values()` this function is not recognize, but `size()` yes.

```java
    JSONObject list = item.getJSONObject("density");
    println("list",list);
    println("list size",list.size());

```

console log

```auto
list {
  "gaz": 1.429,
  "liquid": 1.141
}
list.size() 2

```

but when I try

```auto
    for(Float f : list.values()) {
      println("float", f);
    }

```

the console return

```auto
R_Atom.pde:53:0:53:0: The function values() does not exist.

```

I try the classic way too with

```auto
for(int i = 0 ; i < list.size() ; i++) {
   println(list.get(i));
}

```

```auto
R_Atom.pde:65:0:65:0: The method get(String) in the type JSONObject is not applicable for the arguments (int)

```

So my question is how loop in JSON Object to grab the object ?

---

<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:** [February 24, 2023, 1:13pm UTC](https://discourse.processing.org/t/get-elements-in-jsonobject/41048/2 "2023-02-24T13:13:34Z")

</div>

> [@Stanlepunk](#):
>
> I see the JSONObject is a `HashMap`,

Indeed it relies on a [HashMap](https://processing.org/reference/HashMap.html) internally.

But we’re not supposed to interact w/ it directly b/c it’s [`private`](https://processing.org/reference/private.html):

> <https://github.com/processing/processing/blob/master/core/src/processing/data/JSONObject.java#L179-L183>

> [@Stanlepunk](#):
>
> So my question is how to loop in a JSONObject to grab the object?

This is the whole list of methods for it:  
[http://Processing.GitHub.io/processing-javadocs/core/processing/data/JSONObject.html](http://Processing.GitHub.io/processing-javadocs/core/processing/data/JSONObject.html)

For looping we can use methods [**keyIterator()**](http://processing.github.io/processing-javadocs/core/processing/data/JSONObject.html#keyIterator--) or [**keys()**](http://processing.github.io/processing-javadocs/core/processing/data/JSONObject.html#keys--):

> <https://github.com/processing/processing/blob/master/core/src/processing/data/JSONObject.java#L929-L947>

---

<div class="post-metadata">

**Author:** ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)\
**Post date:** [February 24, 2023, 1:13pm UTC](https://discourse.processing.org/t/get-elements-in-jsonobject/41048/3 "2023-02-24T13:13:54Z")

</div>

I found the solution, but I must will go deep in `Processing core`

```auto
    Object [] objets = list.keys().toArray();
    for(int i = 0 ; i < list.size() ; i++) {
      println("key:",objets[i], ", value:", list.get((String)objets[i]));
    }

```

console

```auto
key: gaz, value: 1.429
key: liquid, value: 1.141

```

May be that’s can be good to add this examples in the Processing documentation and add `keys()` function in the [JSONObject / Reference / Processing.org](https://processing.org/reference/JSONObject.html)

---

<div class="post-metadata">

**Author:** ![Stanlepunk](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/stanlepunk/32/11509_2.png) [@Stanlepunk](https://discourse.processing.org/u/Stanlepunk)\
**Post date:** [February 24, 2023, 1:14pm UTC](https://discourse.processing.org/t/get-elements-in-jsonobject/41048/4 "2023-02-24T13:14:41Z")

</div>

find in the same time 🙂
