How to Parse JSON from URL in Java

In this instructional exercise, you will figure out how to parse JSON from URL in java.
If you don’t know how to parse JSON from URL in java then you are at the right place to know your problem’s solution.

Parse JSON From URL In Java

Let’s start with what JSON is-JSON javascript object notation is simply a data representation format very similar to XML.
It is commonly used for API and configurations. It’s used because of lightweight and easy to read and write. It integrates easily with most of the languages.
Throughout your programming career, you’re going to use JSON all the time whether it’s creating an API or consuming an API or for creating config files for you to use or for other people to use for your application.

Let’s dive into some of the syntax involved in JSON by starting the types that JSON can represent. As we know JSON is a data representation format so we need to able to represent certain data types within it and JSON supports Strings, Numbers, Booleans, Null which represents nothing and Arrays which can be a list of any data types and an Object.

Also learn:

The first thing you need to do is to create a file with the dot JSON extension and that will create your JSON file inside of this file you need to take one of the types that JON supports and you put that inside of your file example you could put a string of numbers or whatever data you want.

Here below is the JSON file named user.json, you can create it by-

//JSON file with json extension i.e.,user.json
    "favourite no":4,
    "is programmer":"true",
               "street":"iel colony",
               "postal code":829112

The below code will help you to achieve your goal-

import java.util.*;  
import org.json.simple.*;  
import org.json.simple.parser.*; 
public class JSONparse 
    public static void main(String[] args) throws Exception  
        // parsing file "user.json" 
        Object object = new JSONParser().parse(new FileReader("http://localhost/BCA_B2/user.json")); 
        JSONObject skr = (JSONObject) object;  
        String Name = (String) skr.get("name"); 
        long favouriteno = (long) skr.get("favourite no"); 
        String isprogrammer=(String) skr.get("is programmer"); 
        long age = (long) skr.get("age"); 
        Map address = ((Map)skr.get("address")); 
        Iterator<Map.Entry> itr1 = address.entrySet().iterator(); 
        while (itr1.hasNext()) { 
            Map.Entry pair =; 
            System.out.println(pair.getKey() + " : " + pair.getValue()); 

Below is the output of the code-

street:iel colony
postal code:829112


3 responses to “How to Parse JSON from URL in Java”

  1. ravindra says:

    is the same code helps us to parse data from api end point

  2. AWieclawski says:

    And what about any direct JSONArray like ex. … ?

  3. Shivansh Potdar says:

    Jsoup can be used to extract the JSON from the tags and then converted to string format and then to JSON from which it can be parsed.

Leave a Reply

Your email address will not be published. Required fields are marked *