Java convert JSON string to object example using Gson fromJson() method

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. Lot of applications have started using JSON for data interchange rather than using XML specially JavaScript frameworks such as ExtJs, jQuery etc. It's the ease of converting Object notations to JSON and vice versa makes it really compelling.

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object. Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of. Please go ahead and download Gson library and add to your your project CLASSPATH if you haven't done so. Here is the link

In this example we are going to learn about the fromJson() method that will help us convert a JSON String to Java Object. This sample Java program reads a JSON String from a file and then converts that to a Java Object. In your application you may be receiving a JSON data as a part of HTTP request.

Input JSON String in file myFile.json

  "customerId": "CUST01",
  "orderId": "101010",
  "orderTotal": 99.99,
  "orderDetailList": [
      "lineId": "1",
      "itemNumber": "ABC",
      "quantity": 9,
      "price": 10.0
      "lineId": "2",
      "itemNumber": "XYZ",
      "quantity": 1,
      "price": 9.99

Java source code for converting a JSON String to Java Object

package com.as400samplecode;

import java.util.ArrayList;

import com.as400samplecode.util.OrderDetail;
import com.as400samplecode.util.OrderHeader;

public class ParseJSON {
 public static void main(String[] args) {

  String myJSONString = "";
  BufferedReader bufferedReader = null;

  try {

   String sCurrentLine;
   bufferedReader = new BufferedReader(new FileReader("data/myFile.json"));

   while ((sCurrentLine = bufferedReader.readLine()) != null) {
    myJSONString = myJSONString + sCurrentLine;

  } catch (IOException e) {
  } finally {
   try {
    if (bufferedReader != null){
   } catch (IOException ex) {
  Gson gson = new Gson();
  OrderHeader orderHeader = gson.fromJson(myJSONString, OrderHeader.class);

  System.out.println("Order Information --->");
  System.out.println("Customer Id: " + orderHeader.getCustomerId());
  System.out.println("Order Id: " + orderHeader.getOrderId());
  System.out.println("Order Total: " + orderHeader.getOrderTotal());
  ArrayList<OrderDetail> orderDetailList = orderHeader.getOrderDetailList();
  for (int i=0; i<orderDetailList.size(); i++){
   System.out.println("Order Detail --->");
   OrderDetail orderDetail = orderDetailList.get(i);
   System.out.println("Line Id: " + orderDetail.getLineId());
   System.out.println("Item Number: " + orderDetail.getItemNumber());
   System.out.println("Quantity: " + orderDetail.getQuantity());
   System.out.println("Price: " + orderDetail.getPrice());


Java source code for OrderHeader Object

package com.as400samplecode.util;

import java.util.ArrayList;

public class OrderHeader {
 String customerId = null;
 String orderId = null;
 Double orderTotal = null;
 ArrayList<OrderDetail> orderDetailList;
 public String getCustomerId() {
  return customerId;
 public void setCustomerId(String customerId) {
  this.customerId = customerId;
 public String getOrderId() {
  return orderId;
 public void setOrderId(String orderId) {
  this.orderId = orderId;
 public Double getOrderTotal() {
  return orderTotal;
 public void setOrderTotal(Double orderTotal) {
  this.orderTotal = orderTotal;
 public ArrayList<OrderDetail> getOrderDetailList() {
  return orderDetailList;
 public void setOrderDetailList(ArrayList<OrderDetail> orderDetailList) {
  this.orderDetailList = orderDetailList;


Java source code for OrderDetail Object

package com.as400samplecode.util;

public class OrderDetail {
 String lineId = null;
 String itemNumber = null;
 int quantity = 0;
 Double price = null;
 public String getLineId() {
  return lineId;
 public void setLineId(String lineId) {
  this.lineId = lineId;
 public String getItemNumber() {
  return itemNumber;
 public void setItemNumber(String itemNumber) {
  this.itemNumber = itemNumber;
 public int getQuantity() {
  return quantity;
 public void setQuantity(int quantity) {
  this.quantity = quantity;
 public Double getPrice() {
  return price;
 public void setPrice(Double price) {
  this.price = price;


Recommended Reading

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.