Looping through JSON properties

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

JSON
"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
"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!

1 Like

I have this for number #2

I had to put { } around it



// 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");
}
2 Likes

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

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

1 Like

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


// 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;
  }
}

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

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

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)

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

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

Or parseJsonobject maybe

2 Likes

Elegant Solution! …but I have problems with that.

Hmm :thinking: Will try!

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");

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

1 Like

uhhh wat :sweat:

Ending JSON
{
	"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…

It looks fine to me.

data.json:

"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:

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

output:

{"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": {}
  }
}}
2 Likes

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

JSON
[
	{"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:

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

2 Likes