How to Get Value from JSON Object in Java Example25 Mar 2025 | 5 min read JSON is a data-interchange format. It is a widely used light-weight and language-independent format. It is capable to convert data from JSON to XML. Java provides a large number of JSON packages. With the help of these packages, we can retrieve or get value from JSONObject. We have used the two different JSON packages org.json and org.json.simple. In this section, we will learn how to get values from JSONObject in Java with a proper example. What is JSONObject in Java?In Java, JSONObject is an unordered collection that contains data in form of name-value pairs. It is denoted as the string enveloped in a pair of curly braces with colons between the names and values. Its internal form is an object having to get and put methods for accessing the values by name and adding and replacing values by name. In the above example, first name, city, and age are names, and John, Florida, and 22 are their values, respectively. Java JSONObject ClassJava provides the JSONObject class that is defined in the org.json package. It denotes an immutable (unchallengeable) JSONObject value. The object value is an unordered group of name-value pairs. It also provides Map to view the JSON object name-value mapping that is unmodifiable. The JSONObject values can be Boolean, String, Number, JSONObject, JSONArray, JSONNumber, JSONString, etc. These JSONObject values can be accessed by using the various corresponding accessor methods. The get() method returns a value if it founds and throws an exception if the value not found. While the opt() method returns the default value instead of throwing the exception. The value may be null. It is the main difference between these two methods. Hence, these two methods are useful to get the value from the JSONObject. We can cast or query an object returned by the generic methods get() and opt(). The JSONObject class contains the following methods that we can use to get values from the JSONObject. getBoolean() MethodThe method parses a parameter name of type String. It returns a Boolean value of the related mapping for the parse's parameter. It returns true if the associated mapping is JsonValue.TRUE and returns false if the associated mapping is JsonValue.FALSE. Syntax: It throws NullPointerException if the specified name has no mapping. It also throws ClassCastException the specified name mapping is not assignable to JsonValue.TRUE or JsonValue.FALSE. getInt()MethodIt is used to get the JsonNumber(name).intValue(). The method parses a parameter name of type String. It returns an integer value of the associated mapping for the parsed parameter. Syntax: It throws NullPointerException if the specified name has no mapping. It also throws ClassCastException the specified name mapping is not assignable to JsonNumber. getJsonArray() MethodIt is used to get the JsonArray get(name). The method parses an argument name of type String whose associated value is to be returned. It returns an array value of the associated mapping for the parseed parameter. It returns null if the object has no mapping for the parameter. Syntax: It throws ClassCastException the specified name mapping is not assignable to JsonArray. getJsonNumber() MethodIt is used to get the (JsonNumber)get(name). The method parses an argument name of type String whose related value is to be returned. It returns the number value of the associated mapping for the parseed parameter. It returns null if the object has no mapping for the parameter. Syntax: It throws ClassCastException the specified name mapping is not assignable to JsonNumber. getJsonObject() MethodIt is used to get the (JsonObject)get(name). The method parses an argument name of type String whose related value is to be returned. It returns the object of the associated mapping for the parse's parameter. It returns null if the object has no mapping for the parameter. Syntax: It throws ClassCastException the specified name mapping is not assignable to JsonObject. getJsonString() MethodIt is used to get the (JsonString)get(name). The method parses an argument name of type String whose related value is to be returned. It returns the String value of the associated mapping for the parsed parameter. It returns null if the object has no mapping for the parameter. Syntax: It throws ClassCastException the specified name mapping is not assignable to JsonString. getString() MethodIt is used to get the getJsonString(name).getString(). The method parses an argument name of type String whose related value is to be returned as String. It returns the String value of the associated mapping for the parsed parameter. Syntax: It throws NullPointerException if the specified name has no mapping. It also throws ClassCastException the specified name mapping is not assignable to JsonString. Let's create a program and get value from the JSON object by using the above methods. Note: Before using the following code, download the json-20200518.jar file and add it to your project.![]() GetValueFromJsonExample.java Output: ![]() Let's use another JSON library and get values from the JSONObject. In the following program, we have used the json-simple library. Before using the following program, download the json-simple-1.1.1.jar file and add it to your project. ![]() GetValueFromJsonObjectExample.java Output: ![]() |
(Usage and Examples) The Java `new` keyword creates a class instance by allocating dynamic memory for a new object and returns a reference to that memory. It can also be used to create an array object. When the `new` keyword is employed, it executes the class...
6 min read
Java supports single-thread as well as multi-thread operations. A single-thread program has a single entry point (the main() method) and an exit point. A multi-thread program has an initial entry point (the main() method), followed by many entry and exit points that are run concurrently with...
7 min read
Elasticsearch is a full-text search and investigation motor in view of Apache Lucene. Elasticsearch makes it more straightforward to perform data aggregation procedures on data from different sources and to perform unstructured queries, for example, Fluffy Hunts, on the put away information. It stores information in a...
6 min read
A sorted binary array is given (an array that only contains 0's and 1's is a binary array). The task is to find the number of ones that are present in the binary sorted array. Example: 1 Input: int arr[] = {0, 0, 0, 0, 1, 1, 1, 1,...
5 min read
Java Swing exists as a segment of Java Foundation Classes (JFC), which delivers a comprehensive set of GUI elements to construct Java desktop applications. Swing offers improved visual features in addition to being lightweight and independent from platform restrictions when compared to AWT (Abstract Window Toolkit). The...
5 min read
Padovan sequence as a tremendous and innovative mathematical sequence that differential from the well-known Fibonacci sequence although has its strong relation. It is said to be defined by a recurrence relation based on the fact that any term is the sum of two terms of the...
5 min read
The Factory Method Design Pattern, as a creational pattern in object-oriented programming, provides an interface for creating the object but leaves the decision as to what class should be instantiated in the concrete subclasses. It is the pattern of loose coupling since the application will not be...
7 min read
In this section, we are going to discuss what is a zigzag array with proper examples. Also, we will create a Java program to convert a simple or ordinary array into a zigzag array and vice-versa. What is a zigzag array? An array is said to be a...
6 min read
The checkedQueue() method in the Java Collections Framework is useful for making queues type safe at the runtime and therefore it is a very effective and important utility and it is for this reason that the checkedQueue() method is in the Collections class and can...
14 min read
Java, a versatile and widely-used programming language, has evolved over the years, offering a rich set of features and functionalities. One of the key aspects that contribute to Java's popularity is its ability to cater to various application types. In this section, we will delve into...
4 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India