Here is the idea :

JSONObject root = new JSONObject(yourJsonString);
JSONArray sportsArray = root.getJSONArray("sports");

// now get the first element:
JSONObject firstSport = sportsArray.getJSONObject(0);

// and details of the first element
String name = firstSport.getString("name"); // basketball
int id = firstSport.getInt("id"); // 40
JSONArray leaguesArray = firstSport.getJSONArray("leagues");

// and so on, you can process leaguesArray similarly

It should work (feel free to complain about compile errors if there are any)

Answer from kiruwka on Stack Overflow
🌐
Reddit
reddit.com › r/javahelp › how to extract data from json file usin java
r/javahelp on Reddit: How to extract data from json file usin Java
August 11, 2020 -

Hi, I'm stuck on json file. How do I data from json file?

Below example, I would like to extract 1.["name" of "user"], 2.["name" of "user" in reply_status], 3.["text" in "reply_status"]

There are other data as well.

Should I use loop?

{
   "replyed_date":"Wed Dec 17",
   "id":545025256960,
   "id_str":"5450256960",
   "text":"hello world",
   "user":{
      "id":275881,
      "id_str":"275881",
      "name":"Json",
    },
   "reply_status":{
      "created":"Wed Dec 16",
      "id":545020558720,
      "id_str":"545058558720",
      "text":"Hello world",
      "user":{
         "id":874136,
         "id_str":"874136",
         "name":"Farmony",
         "screen_name":"Fiftony",
      }
   }
}
{.......
}
Top answer
1 of 3
5
Take a look at Jackson library
2 of 3
1
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
🌐
TutorialsPoint
tutorialspoint.com › how-to-read-the-contents-of-a-json-file-using-java
How can we read a JSON file in Java?
In order to read a JSON file, we need to download the json-simple.jar file and set the path to execute it. import java.io.*; import java.util.*; import org.json.simple.*; import org.json.simple.parser.*; public class JSONReadFromTheFileTest ...
🌐
Blogger
automationreinvented.blogspot.com › 2020 › 05 › how-to-fetch-testdata-from-json-file.html
Automaters: Full Stack QA_SDET: How to extract data from JSON file in Java? Automation/SDET interview question to parse JSON?
There are two ways we can fetch data from the JSON file: ... Let's discuss both the approaches. SDET Interview Question and Answers. Jenkins Interview Questions and Answers. ... Selenium Interview Questions and answers. ... public static String getTestData(String input) throws IOException, ParseException { String testdata; return testdata = (String) getJsonData().get(input);//input is the key } public static JSONObject getJsonData() throws IOException, ParseException { //pass the path of the testdata.json file File filename = new File("TestData//testdata.json"); //convert json file into string String json = FileUtils.readFileToString(filename, "UTF-8"); //parse the string into object Object obj = new JSONParser().parse(json); //give jsonobject o that I can return it to the function everytime it get called JSONObject jsonObject = (JSONObject) obj; return jsonObject; }
🌐
Devstringx
devstringx.com › read-data-from-json
How to Read Data from JSON file Using JAVA? - Devstringx
July 1, 2025 - Now, use JAVA’s input or output classes, namely fileReader or BufferedReader, to read the content. Now you need to parse the JSON data into suitable JAVA object representations. Jsonfactory and ObjectMapperwhere are a few examples of multiple classes and methods for parsing JSON.
🌐
Javatpoint
javatpoint.com › how-to-get-value-from-json-object-in-java-example
How to Get Value from JSON Object in Java Example - Javatpoint
How to Get Value from JSON Object in Java Example with java tutorial, features, history, variables, object, programs, operators, oops concept, array, string, map, math, methods, examples etc.
🌐
Stack Overflow
stackoverflow.com › questions › 62661998 › how-to-extract-values-from-json-file-in-java
How to extract values from JSON file in java? - Stack Overflow
And im interested in extracting the "Price" and "Datetime" values from it. I was trying to use JSON Array and iterate through it, but iterator couldnt work (and i have no idea why). I was able to get the values into a graph using filereader and buffered reader (from simple .txt file filled with only raw price) but not from JSON.
🌐
SAP Community
community.sap.com › t5 › technology-blog-posts-by-members › read-parse-json-data-in-java-mapping-a-step-by-step-guide › ba-p › 13575807
Read/Parse JSON Data in Java Mapping: A step by st... - SAP Community
September 6, 2023 - JSONObject jsonObject2 = new JSONObject(); jsonObject2.put("Name", "XYZ"); jsonObject2.put("City", "MyCity"); System.out.println(jsonObject2.toString());
Find elsewhere
🌐
Coderanch
coderanch.com › t › 558173 › java › Extracting-data-JSON-file-URL
Extracting data from a JSON file/URL/Object/String in Java (Java in General forum at Coderanch)
Daniel: A few things: 1. Java classes start with capitals, so the class should be ReadJson, not readJson. 2. You can't have code outside of a method (hint: see line 10 of your erroring class) 3. Classes don't throw exceptions, methods do. John. ... Well, class names should be nouns, so the name should really be JsonReader. ... Hi yes I realized it and already change it . Thanks for the info I manage to separate the json file, so now I can show each token separately.
🌐
DigitalOcean
digitalocean.com › community › tutorials › jackson-json-java-parser-api-example-tutorial
Jackson JSON Java Parser API Example Tutorial | DigitalOcean
August 3, 2022 - Jackson JSON Parser API provides easy way to convert JSON to POJO Object and supports easy conversion to Map from JSON data. Jackson supports generics too and directly converts them from JSON to object. For our example for JSON to POJO/Java object conversion, we will take a complex example with nested object and arrays. We will use arrays, list and Map in java objects for conversion. Our complex json is stored in a file employee.txt with below structure:
🌐
Stack Overflow
stackoverflow.com › questions › 36695345 › extracting-data-from-a-json-file-in-java
Extracting data from a JSON file in java - Stack Overflow
Then map your json String to your Object (This is an example with Jackson) : ObjectMapper mapper = new ObjectMapper(); //JSON from file to Object Fonction fonction= mapper.readValue(new File("c:\\fonction.json"), Fonction.class); //JSON from ...
🌐
Crunchify
crunchify.com › json tutorials › how to read json object from file in java?
How to Read JSON Object From File in Java? • Crunchify
February 16, 2023 - What’s the best way to load a JSONObject from a json text file? In this Java Example I'll use the same file which we have generated in previous tutorial.
🌐
How to do in Java
howtodoinjava.com › home › java libraries › json.simple – read and write json
JSON.simple - Read and Write JSON in Java
October 1, 2022 - To read JSON from file, we will use the JSON file we created in the previous example. First of all, we will create JSONParser instance to parse JSON file. Use FileReader to read JSON file and pass it to parser.
🌐
Reddit
reddit.com › r/learnprogramming › [java] how can i parse a json string then extract values from it?
r/learnprogramming on Reddit: [Java] How can I parse a JSON string then extract values from it?
July 20, 2015 -

So I have this JSON data given by this link http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7

I'm trying to make a method which can find the maximum temperature on a certain day which would be proved as a parameter.

What is the best way to do this?

The code I have so far is this:

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class WeatherDataParser {

	/**
	 * Given a string of the form returned by the api call:
	 * http://api.openweathermap.org/data/2.5/forecast/daily?q=94043&mode=json&units=metric&cnt=7 
	 * retrieve the maximum temperature for the day indicated by dayIndex
	 * (Note: 0-indexed, so 0 would refer to the first day).
	 */
	public static double getMaxTemperatureForDay(String weatherJsonStr, int dayIndex)
		throws JSONException {
		// TODO: add parsing code here
        
        JSONObject object = new JSONObject(weatherJsonStr);
        double max = object.getDouble("max");
		return max;
	}

}    

I know this code doesn't include dayIndex yet but I was just trying to get the code to be able to recognise where to find the max temperature before I changed it to take into account the day also.

This isn't homework, I'm just trying to learn android development in my own time.

Top answer
1 of 4
3
One would think you'd use the same API you're already using, the one that has JSONArray in it. Are you unable to find or understand the docs for it? Do you understand the format of the data you're getting back? Are you otherwise competent with programming in Java, or are you still unable to do small projects on your own? Do you want pseudocode or real code spoonfed to you, or an algorithm outlined for you, or just some hints about how to do it? The data you get back looks like it might have multiple pieces of data in it, so can you attempt to use a loop here somehow? Do you understand JSON?
2 of 4
1
object that you created refers to the entire JSON that gets returned. That object has 5 properties city, cod, message, cnt, list. That is the reason that you are not able to get max. When you say object.getDouble("max"), it looks through that list of properties for max and does not find it. To get to max from object, you would have to get access to the list array, then get access to the index you wanted, then get access to the temp object; and finally get the property max. JSON object = new JSONObject(weatherJsonStr); JSONArray list = object.getJSONArray('list"); JSONObject firstIndex = list.getJSONObject(0); JSONObject temp = firstIndex.getJSONObject("temp"); double max = temp.getDouble("max"); That is basically how you are going to have to parse the JSON data. You could chain some of these together. I did not actually check this in an IDE, so it might not be completely correct.
🌐
GeeksforGeeks
geeksforgeeks.org › java › parse-json-java
How to parse JSON in Java - GeeksforGeeks
December 23, 2025 - Java does not have built-in JSON parsing support before Java 9, so external libraries are commonly used. In this article, we use JSON.simple, a lightweight and easy-to-use library. Parse JSON from strings or files.
🌐
DZone
dzone.com › coding › java › how to read and parse a json file in java
How to Read and Parse a JSON File in Java
March 6, 2025 - In the above program, the JSONParser().parse() is used, which is present in the org.json.simple.parser.* to parse the File.json file. To see more JSON libraries, you can visit this GitHub page. ... Published at DZone with permission of Mahesh Sharma. See the original article here. Opinions expressed by DZone contributors are their own. Introduction to Polymorphism With Database Engines in NoSQL Using Jakarta NoSQL