Sunday, May 21, 2023

How to iterate over JSONObject in Java to print all key values? Example Tutorial

Hello guys, if you are wondering how to iterate over a JSONObject in Java and print all its fields and values then you have come to the right place. In this article, I will show you how you can print all data from JSONObject in Java. If you know, In json-simple library, one of the top 5 and lightweight JSON library, JSON object is a wrapper class which contains the actual JSON message. In the last article, I have explained how to parse JSON in Java using json-simple library and one question which pops up can we iterate over JSONObject properties? Well, yes, you can iterate over all JSON properties and also get the values from the JSONObject itself. In this article, I'll show you how you can print all keys and value from JSON message using JSONOjbect and Iterator.


Java Program to loop over JSONObject and print all properties

Once you parse your JSON String using JSONParser, you get a JSONObject, but its of no use if you want a POJO i.e. a plain old Java object. For example, if you have following JSON mesage which represent Effective Java book, you ideall want a Java object representing same data:

{
"title": "Effective Java",
"author": "Joshua Bloch",
"price": 42,
"year": 2018,
"edition": 3,
"ratings": 10
}

If you remember we had created an EBook object in our last example about paring JSON with date field in Java, similar to following class:

class EBook{
private String title;
private String author;
private int price;
private int year;
private int edition;
private int ratings

....
}

I have omitted constructor, getter and setter, and essential methods like equals(), hashcode(), and toString() for brevity but you must remember to include a no-argument constructor while creating POJO classes in Java, json-simple might not need that but other JSON parsing library like Jackson and Gson definitely need that. 

How to iterate over JSONObject in Java to print all Attributes? Example Tutorial


Anyway, Here is my sample Java program to demonstrate how you can loop over a JSONObject to print all properties and their values

public static void main(String args[]){

// Iterating over JSONOjbect to print all keys and values
JSONParser parser = new JSONParser();
 
try {
JSONObject json = (JSONObject) parser.parse(JSON);
Set<String> keyset = json.keySet();
Iterator<String> keys = keyset.iterator(); 
while(keys.hasNext()){
String key = keys.next();
Object value = json.get(key);
System.out.println( key +" : " + value);
}

} catch (ParseException e) {
e.printStackTrace();
}

}

}

You can see that the parse() method return us a JSONObject. This object is like a map, so if you need to iterate over all properties, you can ask for set of keys. Similar to java.util.Map it provides a keySet() method which return a Set<String>

We can iterate over this Set to retrieve all keys as shown below:

Iterator<String> keys = keyset.iterator(); 
while(keys.hasNext()){
String key = keys.next();
Object value = json.get(key);
System.out.println( key +" : " + value);
}

Now, if you need value you can just call the JSONObject.get() or JSONObject.getOrDefault() method to get the value. Once you got both key and value, you can simply print them using System.out.println() method. If you want to create an object, you can store these values to pass to the constructor, as I did in last example. 

That's all about how to iterate over JSONObject in Java to print all keys and values. It's very similar to how you iterate over a map to print all key-value pair. You just need to remember to include the json-simple.jar in your classpath if you want to process your JSON Message like this.

No comments :

Post a Comment