JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

How to ignore JSON property using Jackson annotations?


In this example you will know how to play with json property names using Jackson annotation @JsonProperty. Sometimes POJOs contain properties that you do not want to write out, so you can use @JsonIgnore annotation.

Note: Refer How to convert Java object to JSON string? page for dependent libraries.

package com.java2novice.json.models;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonProperty;

public class Employee {

	@JsonProperty("emp_id")
	private int empId;
	
	@JsonProperty("emp_name")
	private String name;
	
	@JsonProperty("emp_designation")
	private String designation;
	
	@JsonIgnore
	private String department;
	
	@JsonIgnore
	private int salary;
	
	public String toString(){
		StringBuilder sb = new StringBuilder();
		sb.append("************************************");
		sb.append("\nempId: ").append(empId);
		sb.append("\nname: ").append(name);
		sb.append("\ndesignation: ").append(designation);
		sb.append("\n************************************");
		return sb.toString();
	}
	
	public int getEmpId() {
		return empId;
	}
	public void setEmpId(int empId) {
		this.empId = empId;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getDesignation() {
		return designation;
	}
	public void setDesignation(String designation) {
		this.designation = designation;
	}
	public String getDepartment() {
		return department;
	}
	public void setDepartment(String department) {
		this.department = department;
	}
	public int getSalary() {
		return salary;
	}
	public void setSalary(int salary) {
		this.salary = salary;
	}
}

In another way you can use @JsonIgnoreProperties annotation to skip pojo properties. Here is the code snippet: You can use either way.

package com.java2novice.json.models;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;

@JsonIgnoreProperties({ "department", "salary" })
public class Employee {

	@JsonProperty("emp_id")
	private int empId;
	
	@JsonProperty("emp_name")
	private String name;
	
	@JsonProperty("emp_designation")
	private String designation;
	
	private String department;
	
	private int salary;
	
 	/*** Rest of the code is common from the above class**/	
}

Here is the example which reads json string value from a file, maps it to the object and converts the object back to the json string format.

package com.java2novice.json.examples;

import java.io.File;
import java.io.IOException;

import org.codehaus.jackson.map.ObjectMapper;

import com.java2novice.json.models.Employee;

public class JsonToObject {
	
	public static void main(String a[]){
		
		ObjectMapper mapper = new ObjectMapper();
		try {
			// reading json input from the file and mapping to object
			File jsonInputFile = new File("C:\\jsonInput.txt");
			Employee emp = mapper.readValue(jsonInputFile, Employee.class);
			System.out.println(emp);
			// converting object to string value
			String jsonStr 
					= mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
			System.out.println(jsonStr);
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

jsonInput.txt content:

{
    "emp_id": 1017,
    "emp_name": "Nagesh Y",
    "emp_designation": "Manager"
}

Output:
************************************
empId: 1017
name: Nagesh Y
designation: Manager
************************************
{
  "emp_id" : 1017,
  "emp_name" : "Nagesh Y",
  "emp_designation" : "Manager"
}
<< Previous Program | Next Program >>

Jackson JSON examples

  1. How to convert Java object to JSON string?
  2. How to convert JSON string to Java object?
  3. How to convert JSON string to Map using Jackson API?
  4. How to convert Map to JSON string using Jackson API?
  5. Enable JSON pretty print using Jackson API
  6. How to rename JSON properties using Jackson annotations?
  7. How to ignore JSON property using Jackson annotations?
  8. How to order JSON elements using Jackson annotations?
  9. How to ignore json empty or null values using Jackson API in java?
  10. How to handle date in Json using Jackson api in java?
  11. How to read specific json node in Jackson api (tree model)?
  12. Jackson API client - how to read json from URL?
Knowledge Centre
What is abstract class or abstract method?
We cannot create instance for an abstract class. We can able to create instance for its subclass only. By specifying abstract keyword just before class, we can make a class as abstract class.

public abstract class MyAbstractClass{

}

Abstract class may or may not contains abstract methods. Abstract method is just method signature, it does not containes any implementation. Its subclass must provide implementation for abstract methods. Abstract methods are looks like as given below:

public abstract int getLength();
Famous Quotations
Be yourself; everyone else is already taken.
-- Oscar Wilde

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java™ Platform Standard Ed. 8 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2022 by Nataraja Gootooru. All Rights Reserved.