Did you mean: json parser java

The org.json library is easy to use.

Just remember (while casting or using methods like getJSONObject and getJSONArray) that in JSON notation

  • [ … ] represents an array, so library will parse it to JSONArray
  • { … } represents an object, so library will parse it to JSONObject

Example code below:

import org.json.*;

String jsonString = ... ; //assign your JSON String here
JSONObject obj = new JSONObject(jsonString);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");

JSONArray arr = obj.getJSONArray("posts"); // notice that `"posts": [...]`
for (int i = 0; i < arr.length(); i++)
{
    String post_id = arr.getJSONObject(i).getString("post_id");
    ......
}

You may find more examples from: Parse JSON in Java

Downloadable jar: http://mvnrepository.com/artifact/org.json/json

Answer from user1931858 on Stack Overflow
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › stream › JsonParser.html
JsonParser (Java(TM) EE 7 Specification APIs)
javax.json.stream · All Superinterfaces: AutoCloseable, Closeable · public interface JsonParser extends Closeable · Provides forward, read-only access to JSON data in a streaming way. This is the most efficient way for reading JSON data. The class Json contains methods to create parsers ...
Top answer
1 of 16
962

The org.json library is easy to use.

Just remember (while casting or using methods like getJSONObject and getJSONArray) that in JSON notation

  • [ … ] represents an array, so library will parse it to JSONArray
  • { … } represents an object, so library will parse it to JSONObject

Example code below:

import org.json.*;

String jsonString = ... ; //assign your JSON String here
JSONObject obj = new JSONObject(jsonString);
String pageName = obj.getJSONObject("pageInfo").getString("pageName");

JSONArray arr = obj.getJSONArray("posts"); // notice that `"posts": [...]`
for (int i = 0; i < arr.length(); i++)
{
    String post_id = arr.getJSONObject(i).getString("post_id");
    ......
}

You may find more examples from: Parse JSON in Java

Downloadable jar: http://mvnrepository.com/artifact/org.json/json

2 of 16
714

For the sake of the example lets assume you have a class Person with just a name.

private class Person {
    public String name;

    public Person(String name) {
        this.name = name;
    }
}

Jackson (Maven)

My personal favourite and probably the most widely used.

ObjectMapper mapper = new ObjectMapper();

// De-serialize to an object
Person user = mapper.readValue("{\"name\": \"John\"}", Person.class);
System.out.println(user.name); //John

// Read a single attribute
JsonNode nameNode = mapper.readTree("{\"name\": \"John\"}");
System.out.println(nameNode.get("name").asText());

Google GSON (Maven)

Gson g = new Gson();

// De-serialize to an object
Person person = g.fromJson("{\"name\": \"John\"}", Person.class);
System.out.println(person.name); //John

// Read a single attribute
JsonObject jsonObject = new JsonParser().parseString("{\"name\": \"John\"}").getAsJsonObject();
System.out.println(jsonObject.get("name").getAsString()); //John

Org.JSON (Maven)

This suggestion is listed here simply because it appears to be quite popular due to stackoverflow reference to it. I would not recommend using it as it is more a proof-of-concept project than an actual library.

JSONObject obj = new JSONObject("{\"name\": \"John\"}");

System.out.println(obj.getString("name")); //John
Discussions

java - How to get the underlying String from a JsonParser (Jackson Json) - Stack Overflow
Looking through the documentation and source code I don't see a clear way to do this. Curious if I'm missing something. Say I receive an InputStream from a server response. I create a JsonParser f... More on stackoverflow.com
🌐 stackoverflow.com
java - Looking for JsonParser dependency - Stack Overflow
I have worked out that JsonParser is in javax.json.stream but i have no idea where i can get a hold of it. More on stackoverflow.com
🌐 stackoverflow.com
Parsing a JSON file in Java using json-simple - Stack Overflow
JSONParser parser = new JSONParser(); Object obj = parser.parse(new FileReader("...")); //the location of the file JSONObject jsonObject = (JSONObject) obj; JSONArray numbers = (JSONArray) jsonObject.get("numbers"); for (Object number : numbers) { JSONObject jsonNumber = (JSONObject) number; ... More on stackoverflow.com
🌐 stackoverflow.com
Best Java JSON Parser: Gson or Jackson?

Jackson, but mostly because I tend to use it from Spring MVC and it just works without any extra effort.

More on reddit.com
🌐 r/java
61
47
April 27, 2015
🌐
Medium
medium.com › @AlexanderObregon › javas-jsonparser-parse-method-explained-76b126f6110e
Java’s JsonParser.parse() Method Explained | Medium
October 13, 2024 - The JsonParser class in Java, part of the javax.json package, is designed to parse JSON-formatted data. The parse() method plays an important role in reading JSON data from various sources, including strings, files, or network responses.
Top answer
1 of 5
30

If you have the JsonParser then you can use jsonParser.readValueAsTree().toString().

However, this likely requires that the JSON being parsed is indeed valid JSON.

2 of 5
6

I had a situation where I was using a custom deserializer, but I wanted the default deserializer to do most of the work, and then using the SAME json do some additional custom work. However, after the default deserializer does its work, the JsonParser object current location was beyond the json text I needed. So I had the same problem as you: how to get access to the underlying json string.

You can use JsonParser.getCurrentLocation.getSourceRef() to get access to the underlying json source. Use JsonParser.getCurrentLocation().getCharOffset() to find the current location in the json source.

Here's the solution I used:

public class WalkStepDeserializer extends StdDeserializer<WalkStep> implements
    ResolvableDeserializer {

    // constructor, logger, and ResolvableDeserializer methods not shown

    @Override
    public MyObj deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException,
            JsonProcessingException {
        MyObj myObj = null;

        JsonLocation startLocation = jp.getCurrentLocation();
        long charOffsetStart = startLocation.getCharOffset();

        try {
            myObj = (MyObj) defaultDeserializer.deserialize(jp, ctxt);
        } catch (UnrecognizedPropertyException e) {
            logger.info(e.getMessage());
        }

        JsonLocation endLocation = jp.getCurrentLocation();
        long charOffsetEnd = endLocation.getCharOffset();
        String jsonSubString = endLocation.getSourceRef().toString().substring((int)charOffsetStart - 1, (int)charOffsetEnd);
        logger.info(strWalkStep);

        // Special logic - use JsonLocation.getSourceRef() to get and use the entire Json
        // string for further processing

        return myObj;
    }
}

And info about using a default deserializer in a custom deserializer is at How do I call the default deserializer from a custom deserializer in Jackson

🌐
Baeldung
baeldung.com › home › json › using static methods instead of deprecated jsonparser
Using Static Methods Instead of Deprecated JsonParser | Baeldung
June 20, 2024 - We can parse a JSON string directly into JsonObject without using a deprecated instance of JsonParser using the parseString() static method.
🌐
GitHub
github.com › fangyidong › json-simple › blob › master › src › main › java › org › json › simple › parser › JSONParser.java
json-simple/src/main/java/org/json/simple/parser/JSONParser.java at master · fangyidong/json-simple
A simple Java toolkit for JSON. You can use json-simple to encode or decode JSON text. - json-simple/src/main/java/org/json/simple/parser/JSONParser.java at master · fangyidong/json-simple
Author   fangyidong
Find elsewhere
🌐
Javadoc.io
javadoc.io › doc › com.fasterxml.jackson.core › jackson-core › latest › com › fasterxml › jackson › core › JsonParser.html
JsonParser - jackson-core 2.21.1 javadoc
Bookmarks · Latest version of com.fasterxml.jackson.core:jackson-core · https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-core · Current version 2.21.1 · https://javadoc.io/doc/com.fasterxml.jackson.core/jackson-core/2.21.1 · package-list path (used for javadoc generation -link ...
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 2.8 › com › fasterxml › jackson › core › JsonParser.html
JsonParser (Jackson-core 2.8.0 API)
If current token is of type ... returns one of JsonParser.NumberType constants; otherwise returns null. ... Numeric accessor that can be called when the current token is of type JsonToken.VALUE_NUMBER_INT and it can be expressed as a value of Java byte primitive ...
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 2.6 › com › fasterxml › jackson › core › JsonParser.html
JsonParser (Jackson-core 2.6.0 API)
If current token is of type ... returns one of JsonParser.NumberType constants; otherwise returns null. ... Numeric accessor that can be called when the current token is of type JsonToken.VALUE_NUMBER_INT and it can be expressed as a value of Java byte primitive ...
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 2.5 › com › fasterxml › jackson › core › JsonParser.html
JsonParser (Jackson-core 2.5.0 API)
If current token is of type ... returns one of JsonParser.NumberType constants; otherwise returns null. ... Numeric accessor that can be called when the current token is of type JsonToken.VALUE_NUMBER_INT and it can be expressed as a value of Java byte primitive ...
🌐
JAXB
javaee.github.io › javaee-spec › javadocs › javax › json › stream › JsonParser.html
JsonParser (Java(TM) EE 8 Specification APIs)
javax.json.stream · All Superinterfaces: AutoCloseable, Closeable · public interface JsonParser extends Closeable · Provides forward, read-only access to JSON data in a streaming way. This is the most efficient way for reading JSON data. This is the only way to parse and process JSON data ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › parse-json-java
How to parse JSON in Java - GeeksforGeeks
December 23, 2025 - import java.io.FileReader; import java.util.Iterator; import java.util.Map; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; public class JSONReadExample { public static void main(String[] args) throws Exception { Object obj = new JSONParser().parse(new FileReader("JSONExample.json")); JSONObject jo = (JSONObject) obj; String firstName = (String) jo.get("firstName"); String lastName = (String) jo.get("lastName"); long age = (long) jo.get("age"); System.out.println(firstName); System.out.println(lastName); System.out.println(age); Ma
🌐
Javadoc.io
javadoc.io › doc › com.google.code.gson › gson › latest › com.google.gson › com › google › gson › JsonParser.html
JsonParser - gson 2.13.2 javadoc
Bookmarks · Latest version of com.google.code.gson:gson · https://javadoc.io/doc/com.google.code.gson/gson · Current version 2.13.2 · https://javadoc.io/doc/com.google.code.gson/gson/2.13.2 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc/com.go...
🌐
DigitalOcean
digitalocean.com › community › tutorials › jackson-json-java-parser-api-example-tutorial
Jackson JSON Java: Parser API Examples & Tutorial | DigitalOcean
August 3, 2022 - JsonParser is the jackson json streaming API to read json data, we are using it to read data from the file and then parseJSON() method is used to loop through the tokens and process them to create our java object. Notice that parseJSON() method is called recursively for “address” because ...
🌐
GitHub
github.com › FasterXML › jackson-1 › blob › master › src › java › org › codehaus › jackson › JsonParser.java
jackson-1/src/java/org/codehaus/jackson/JsonParser.java at master · FasterXML/jackson-1
import java.util.Iterator; · import org.codehaus.jackson.type.TypeReference; · /** * Base class that defines public API for reading JSON content. * Instances are created using factory methods of · * a {@link JsonFactory} instance. * * @author Tatu Saloranta · */ public abstract class JsonParser ·
Author   FasterXML
🌐
Javadoc.io
javadoc.io › doc › com.google.code.gson › gson › 2.8.5 › com › google › gson › JsonParser.html
JsonParser - gson 2.8.5 javadoc
Bookmarks · Latest version of com.google.code.gson:gson · https://javadoc.io/doc/com.google.code.gson/gson · Current version 2.8.5 · https://javadoc.io/doc/com.google.code.gson/gson/2.8.5 · package-list path (used for javadoc generation -link option) · https://javadoc.io/doc/com.goog...