All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

How To Get Values From JSON Array In Java

Last Updated : Mar 11, 2024

How To Get Values From JSON Array In Java

In this article we will show you the solution of how to get values from JSON array in java, a JSON array is an ordered group of values that are denoted by square brackets, starting with the character "[" and ending with the character "]".

The arrays' values are separated by commas, or ",". JSON objects are processed using the little library known as json-simple.

JSON documents can be read or written by Java programs using this method.

We will now discuss the idea of how to get values from json array in java with an example.

Step By Step Guide On How To Get Values From JSON Array In Java :-

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Iterator;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
public class talkerscode{
   public static void main(String args[]) {
      JSONParser jsonParser = new JSONParser();
      try {
         JSONObject jsonObject = (JSONObject) jsonParser.parse(new FileReader("E:/demo.json"));
         System.out.println("contents of the JSON are: ");
         System.out.println("ID: "+jsonObject.get("ID"));
         System.out.println("First name: "+jsonObject.get("First_Name"));
         System.out.println("Last name: "+jsonObject.get("Last_Name"));
         JSONArray jsonArray = (JSONArray) jsonObject.get("contact");
         System.out.println("");
         System.out.println("Contact details: ");
         Iterator<String> iterator = jsonArray.iterator();
         while(iterator.hasNext()) {
            System.out.println(iterator.next());
         }
      } catch (FileNotFoundException e) {
         e.printstacktrace();
      } catch (IOException e) {
            e.printstacktrace();
      } catch (ParseException e) {
            e.printstacktrace();
      }
   }
}
  1. We import the classes required for decoding JSON objects.
  2. A fresh JSONParser object is created.
  3. To read the JSON file provided as a FileReader, we use the parse method of the JSONParser object.
  4. We create a new JSONObject variable and assign the parsed JSON object to it.
  5. The get function on the JSONObject variable is used to retrieve each field in order to print the JSON object's contents.
  6. From the JSONObject, we extract the contact information array and allocate it to a fresh JSONArray variable.
  7. The contact information is printed.
  8. We loop through the contact details array using an iterator, printing each element as we go.
  9. We deal with a variety of exceptions that could be thrown while parsing and reading JSON files.

Conclusion :-

As a result, we have successfully learned how to get values from json array in java with an example.

This short post taught us how to extract all of the mapped values for a given key from a JSONArray.

The JSON-Java (org.json) library was utilized in this case. Another similar and capable option for using JSON in Java is JSON.simple. Feel free to look around.

I hope this article on how to get values from JSON array in java helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Pragati

Experienced coding content writer who enjoys breaking down complex concepts in programming languages like Java, Python, C, and C++. Over three years of experience producing interesting and relevant content for a variety of entities. Committed to providing concise and easy-to-understand articles that assist readers in easily understanding technology and industry trends in the world of coding and software development.

Follow Pragati On Linkedin 🡪