# Looping through JSON properties

**URL:** <https://discourse.processing.org/t/looping-through-json-properties/19090>\
**Category:** Coding Questions\
**Created:** [March 27, 2020, 5:04pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090 "2020-03-27T17:04:13Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [March 27, 2020, 5:04pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/1 "2020-03-27T17:04:13Z")

</div>

I need to loop through a JSON file that looks like this:

> **JSON**
>
> ```auto
> "blocks":{
> "vJb19r)WPw88AL7K#^ZI":{
> "opcode":"control_incr_counter",
> "next":"IV.E7vBXTcdK0jZYX}(o",
> "parent":"lFI}(?:VU3KYI0bg*{_@",
> "inputs":{},
> "fields":{},
> },
> "IV.E7vBXTcdK0jZYX}(o":{
> "opcode":"control_get_counter",
> "next":"FT-ZWL](%!N-(;E6S{1-",
> "parent":"vJb19r)WPw88AL7K#^ZI",
> "inputs":{},
> "fields":{},
> },
> "lFI}(?:VU3KYI0bg*{_@":{
> "opcode":"control_wait",
> "next":"vJb19r)WPw88AL7K#^ZI",
> "parent":null,
> "inputs":{},
> "fields":{},
> },
> "FT-ZWL](%!N-(;E6S{1-":{
> "opcode":"control_all_at_once",
> "next":null,
> "parent":"IV.E7vBXTcdK0jZYX}(o",
> "inputs":{},
> "fields":{},
> }
> 
> ```

> **Simpler & Shorter JSON**
>
> ```auto
> "blocks":{
> "vJb19r)WPw88AL7K#^ZI":{},
> "IV.E7vBXTcdK0jZYX}(o":{},
> "lFI}(?:VU3KYI0bg*{_@":{},
> "FT-ZWL](%!N-(;E6S{1-":{}
> 
> ```

I can loop through a JSON array, but I can’t find a way to loop through this.  
Please send help!

---

<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:** [March 27, 2020, 6:34pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/2 "2020-03-27T18:34:54Z")

</div>

I have this for number #2

I had to put { } around it

```auto

// The short JSON file called "data.json" is parsed 
// in the code below. It must be in the project's "data" folder.

JSONObject json;

void setup() {

  json = loadJSONObject("data.json");

  JSONObject id = json.getJSONObject("blocks");

  println(id );
  println(id.size() );

  JSONObject animal1 = id.getJSONObject("vJb19r)WPw88AL7K#^ZI"); 

  println(animal1);

  //String species = json.getString("species");
  //String name = json.getString("name");
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 27, 2020, 6:46pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/3 "2020-03-27T18:46:02Z")

</div>

The key issue here is that `"blocks"` isn’t a JSONArray or a JSONObject – it is a key:value pair from inside a JSONObject.

Check out the diagram here and look for the `:` to see why:

[https://www.json.org/json-en.html](https://www.json.org/json-en.html)

That is why you need to add `{` `}` as @Chrisir says to make it a valid object.

---

<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:** [March 27, 2020, 6:49pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/4 "2020-03-27T18:49:38Z")

</div>

for the first json

here { … }} is missing I think

and the next data is the name of the next data block.

I tried to do a while loop for that.

Chrisir

```auto

// The following short JSON file called "data.json" is parsed 
// in the code below. It must be in the project's "data" folder.

JSONObject json;

void setup() {

  size(222, 333); 

  json = loadJSONObject("data.json");

  JSONObject id = json.getJSONObject("blocks");

  println(id );
  println(id.size() );

  JSONObject animal1 = id.getJSONObject("vJb19r)WPw88AL7K#^ZI"); 
  println(animal1);

  String species = animal1.getString("opcode");
  println(species);

  println("==================================================================");

  String next1 = animal1.getString("next");
  println(next1);

  boolean abort=false; 
  while (next1!=null && !abort) {
    // load full item
    animal1 = id.getJSONObject(next1);
    println(animal1);

    species = animal1.getString("opcode");
    println(species);

    if (!animal1.isNull("next")) 
      next1 = animal1.getString("next");
    else abort = true;
  }
}

```

---

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [March 27, 2020, 7:02pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/5 "2020-03-27T19:02:12Z")

</div>

> [@Chrisir](#):
>
> JSONObject animal1 = id.getJSONObject(“vJb19r)WPw88AL7K#^ZI”);

This would work if I knew the id of one of them, the problem is I don’t.

---

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [March 27, 2020, 7:11pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/6 "2020-03-27T19:11:33Z")

</div>

> [@jeremydouglass](#):
>
> The key issue here is that `"blocks"` isn’t a JSONArray or a JSONObject – it is a key:value pair from inside a JSONObject.

Yup! there’s an _extremely_ easy way in p5js/js [for (x in JSON)] but not processing.

---

<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:** [March 27, 2020, 7:13pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/7 "2020-03-27T19:13:46Z")

</div>

I was wonering that too

As I said when you know the first, the next value might give you the name of the next

I tried to for loop over the structure and using getjsonobject(i) but that’s only allowed with jsonarray. Or so it seems.

Maybe you can convert it to a jsonarray and then use a for loop with i (without knowing the name)

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 27, 2020, 7:14pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/8 "2020-03-27T19:14:20Z")

</div>

Well, I mean, the easy way in Processing is something like (untested):

```auto
String brokenJSON = loadStrings("data.json");
JSONObject json = parseJSONObject("{"+brokenJSON+"}"); // not loadJSONObject !

```

---

<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:** [March 27, 2020, 7:14pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/9 "2020-03-27T19:14:59Z")

</div>

> [@jeremydouglass](#):
>
> json = loadJSONObject(“data.json”);

Or parseJsonobject maybe

---

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [March 27, 2020, 7:15pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/10 "2020-03-27T19:15:16Z")

</div>

> [@Chrisir](#):
>
> Maybe you can convert it to a jsonarray

Elegant Solution! …but I have problems with that.

---

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [March 27, 2020, 7:15pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/11 "2020-03-27T19:15:49Z")

</div>

Hmm 🤔 Will try!

---

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [March 27, 2020, 7:21pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/12 "2020-03-27T19:21:12Z")

</div>

> [@jeremydouglass](#):
>
> String brokenJSON = loadStrings(“data.json”);

Error: Type mismatch, “java.lang.String” does not match with “java.lang.String”  
I can fix this.

it should be `String[] brokenJSON = loadStrings("temp.json");`

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 27, 2020, 7:24pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/13 "2020-03-27T19:24:03Z")

</div>

Yes, sorry – I said the solution was untested. I should have actually tried it first before sharing, but I was away from my computer.

Once you get your Strings[], join them with linebreaks using join:

[https://processing.org/reference/join\_.html](https://processing.org/reference/join_.html)

---

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [March 27, 2020, 8:04pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/14 "2020-03-27T20:04:08Z")

</div>

uhhh wat 😓

> **Ending JSON**
>
> ```auto
> {
> "FR~WswQhbii~]3J+yf_K": {},
> ";;KDvlbO_J)iC@L7r/vO": {},
> "5_f,lK.ZmU`(~J2]hDud": {},
> "7*Y@oJ2l6v@xbXs*q^,9": {},
> "MV7EFXv(vh~-]7KpHk5l": {},
> "POz8mef]5jM@=6?T~O*(": {},
> "Wtr[S5a|eT%`%Dp?/[Hx": {},
> "C8iM#|e?~`[*F+L}:x:+": {},
> "6KXsMZuh7e:@V_d]v8qG": {}
> }
> 
> ```

i don’t think it did anything…

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [March 27, 2020, 9:21pm UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/15 "2020-03-27T21:21:24Z")

</div>

It looks fine to me.

data.json:

```auto
"blocks":{
  "vJb19r)WPw88AL7K#^ZI":{
   "opcode":"control_incr_counter",
   "next":"IV.E7vBXTcdK0jZYX}(o",
   "parent":"lFI}(?:VU3KYI0bg*{_@",
   "inputs":{},
   "fields":{},
  },
  "IV.E7vBXTcdK0jZYX}(o":{
   "opcode":"control_get_counter",
   "next":"FT-ZWL](%!N-(;E6S{1-",
   "parent":"vJb19r)WPw88AL7K#^ZI",
   "inputs":{},
   "fields":{},
  },
  "lFI}(?:VU3KYI0bg*{_@":{
   "opcode":"control_wait",
   "next":"vJb19r)WPw88AL7K#^ZI",
   "parent":null,
   "inputs":{},
   "fields":{},
  },
  "FT-ZWL](%!N-(;E6S{1-":{
   "opcode":"control_all_at_once",
   "next":null,
   "parent":"IV.E7vBXTcdK0jZYX}(o",
   "inputs":{},
   "fields":{},
  }
}

```

BrokenJSONObject.pde:

```auto
String[] brokenJSON = loadStrings("data.json");
String jsonString = "{" + join(brokenJSON, '\n') + "}";
JSONObject json = parseJSONObject(jsonString);
println(json);

```

output:

```auto
{"blocks": {
  "vJb19r)WPw88AL7K#^ZI": {
    "next": "IV.E7vBXTcdK0jZYX}(o",
    "parent": "lFI}(?:VU3KYI0bg*{_@",
    "inputs": {},
    "opcode": "control_incr_counter",
    "fields": {}
  },
  "lFI}(?:VU3KYI0bg*{_@": {
    "next": "vJb19r)WPw88AL7K#^ZI",
    "parent": null,
    "inputs": {},
    "opcode": "control_wait",
    "fields": {}
  },
  "IV.E7vBXTcdK0jZYX}(o": {
    "next": "FT-ZWL](%!N-(;E6S{1-",
    "parent": "vJb19r)WPw88AL7K#^ZI",
    "inputs": {},
    "opcode": "control_get_counter",
    "fields": {}
  },
  "FT-ZWL](%!N-(;E6S{1-": {
    "next": null,
    "parent": "IV.E7vBXTcdK0jZYX}(o",
    "inputs": {},
    "opcode": "control_all_at_once",
    "fields": {}
  }
}}

```

---

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [March 28, 2020, 9:27am UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/16 "2020-03-28T09:27:44Z")

</div>

ok now “blocks” is an attribute, but I can’t fathom how that helps!  
Ideally I have this:

> **JSON**
>
> ```auto
> [
> {"ID":"FR~WswQhbii~]3J+yf_K", },
> {"ID":KDvlbO_J)iC@L7r/vO", },
> {"ID":"5_f,lK.ZmU`(~J2]hDud", },
> {"ID":"7*Y@oJ2l6v@xbXs*q^,9", },
> {"ID":"MV7EFXv(vh~-]7KpHk5l", },
> {"ID":"POz8mef]5jM@=6?T~O*(", },
> {"ID":"Wtr[S5a|eT%`%Dp?/[Hx", },
> {"ID":"C8iM#|e?~`[*F+L}:x:+", },
> {"ID":"6KXsMZuh7e:@V_d]v8qG", }
> ]
> 
> ```

But now don’t I still have attributes?  
Like what you uploaded:

> [@jeremydouglass](#):
>
> ```auto
> {
> "blocks": {
> "vJb19r)WPw88AL7K#^ZI": {
> "next": "IV.E7vBXTcdK0jZYX}(o",
> "parent": "lFI}(?:VU3KYI0bg*{_@",
> "inputs": {},
> "opcode": "control_incr_counter",
> "fields": {}
> },
> "lFI}(?:VU3KYI0bg*{_@": {
> "next": "vJb19r)WPw88AL7K#^ZI",
> "parent": null,
> "inputs": {},
> "opcode": "control_wait",
> "fields": {}
> },
> "IV.E7vBXTcdK0jZYX}(o": {
> "next": "FT-ZWL](%!N-(;E6S{1-",
> "parent": "vJb19r)WPw88AL7K#^ZI",
> "inputs": {},
> "opcode": "control_get_counter",
> "fields": {}
> },
> "FT-ZWL](%!N-(;E6S{1-": {
> "next": null,
> "parent": "IV.E7vBXTcdK0jZYX}(o",
> "inputs": {},
> "opcode": "control_all_at_once",
> "fields": {}
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Odermonicon](https://avatars.discourse-cdn.com/v4/letter/o/96bed5/32.png) [@Odermonicon](https://discourse.processing.org/u/Odermonicon)\
**Post date:** [March 28, 2020, 11:23am UTC](https://discourse.processing.org/t/looping-through-json-properties/19090/17 "2020-03-28T11:23:28Z")

</div>

I found the solution!  
`jsonObject.keys()` Returns a list of keys as an array.
