How to parse JSON in Java


I have the following JSON text. How can I parse it to get the values of pageName, pagePic, post_id, etc.?

   "pageInfo": {
         "pageName": "abc",
         "pagePic": ""
    "posts": [
              "post_id": "123456789012_123456789012",
              "actor_id": "1234567890",
              "picOfPersonWhoPosted": "",
              "nameOfPersonWhoPosted": "Jane Doe",
              "message": "Sounds cool. Can't wait to see it!",
              "likesCount": "2",
              "comments": [],
              "timeOfPost": "1234567890"
3/15/2019 4:20:31 PM

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) { = name;

Google GSON (Maven)

My personal favourite as to the great JSON serialisation / de-serialisation of objects.

Gson g = new Gson();

Person person = g.fromJson("{\"name\": \"John\"}", Person.class);
System.out.println(; //John

System.out.println(g.toJson(person)); // {"name":"John"}


If you want to get a single attribute out you can do it easily with the Google library as well:

JsonObject jsonObject = new JsonParser().parse("{\"name\": \"John\"}").getAsJsonObject();

System.out.println(jsonObject.get("name").getAsString()); //John

Org.JSON (Maven)

If you don't need object de-serialisation but to simply get an attribute, you can try org.json (or look GSON example above!)

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

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

Jackson (Maven)

ObjectMapper mapper = new ObjectMapper();
Person user = mapper.readValue("{\"name\": \"John\"}", Person.class);

System.out.println(; //John

  1. If one wants to create Java object from JSON and vice versa, use GSON or JACKSON third party jars etc.

    //from object to JSON 
    Gson gson = new Gson();
    // from JSON to object 
    yourObject o = gson.fromJson(JSONString,yourObject.class);
  2. But if one just want to parse a JSON string and get some values, (OR create a JSON string from scratch to send over wire) just use JaveEE jar which contains JsonReader, JsonArray, JsonObject etc. You may want to download the implementation of that spec like javax.json. With these two jars I am able to parse the json and use the values.

    These APIs actually follow the DOM/SAX parsing model of XML.

    Response response = request.get(); // REST call 
        JsonReader jsonReader = Json.createReader(new StringReader(response.readEntity(String.class)));
        JsonArray jsonArray = jsonReader.readArray();
        ListIterator l = jsonArray.listIterator();
        while ( l.hasNext() ) {
              JsonObject j = (JsonObject);
              JsonObject ciAttr = j.getJsonObject("ciAttributes");

quick-json parser is very straightforward, flexible, very fast and customizable. Try it


  • Compliant with JSON specification (RFC4627)
  • High-Performance JSON parser
  • Supports Flexible/Configurable parsing approach
  • Configurable validation of key/value pairs of any JSON Hierarchy
  • Easy to use # Very small footprint
  • Raises developer friendly and easy to trace exceptions
  • Pluggable Custom Validation support - Keys/Values can be validated by configuring custom validators as and when encountered
  • Validating and Non-Validating parser support
  • Support for two types of configuration (JSON/XML) for using quick-JSON validating parser
  • Requires JDK 1.5
  • No dependency on external libraries
  • Support for JSON Generation through object serialisation
  • Support for collection type selection during parsing process

It can be used like this:

JsonParserFactory factory=JsonParserFactory.getInstance();
JSONParser parser=factory.newJsonParser();
Map jsonMap=parser.parseJson(jsonString);

You could use Google Gson.

Using this library you only need to create a model with the same JSON structure. Then the model is automatically filled in. You have to call your variables as your JSON keys, or use @SerializedName if you want to use different names.


From your example:

    "pageInfo": {
        "pageName": "abc",
        "pagePic": ""
    "posts": [
            "post_id": "123456789012_123456789012",
            "actor_id": "1234567890",
            "picOfPersonWhoPosted": "",
            "nameOfPersonWhoPosted": "Jane Doe",
            "message": "Sounds cool. Can't wait to see it!",
            "likesCount": "2",
            "comments": [],
            "timeOfPost": "1234567890"


class MyModel {

    private PageInfo pageInfo;
    private ArrayList<Post> posts = new ArrayList<>();

class PageInfo {

    private String pageName;
    private String pagePic;

class Post {

    private String post_id;

    @SerializedName("actor_id") // <- example SerializedName
    private String actorId;

    private String picOfPersonWhoPosted;
    private String nameOfPersonWhoPosted;
    private String message;
    private String likesCount;
    private ArrayList<String> comments;
    private String timeOfPost;


Now you can parse using Gson library:

MyModel model = gson.fromJson(jsonString, MyModel.class);

Gradle import

Remember to import the library in the app Gradle file

implementation '' // or earlier versions

Automatic model generation

You can generate model from JSON automatically using online tools like this.


Almost all the answers given requires a full deserialization of the JSON into a Java object before accessing the value in the property of interest. Another alternative, which does not go this route is to use JsonPATH which is like XPath for JSON and allows traversing of JSON objects.

It is a specification and the good folks at JayWay have created a Java implementation for the specification which you can find here:

So basically to use it, add it to your project, eg:


and to use:

String pageName =, "$.pageInfo.pageName");
String pagePic =, "$.pageInfo.pagePic");
String post_id =, "$.pagePosts[0].post_id");


Check the JsonPath specification page for more information on the other ways to transverse JSON.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Email: [email protected]