JSON Basics

  • It stands for JavaScript Object Notation
  • Its a lightweight data format for storing and exchanging data
  • Language independent so it can be used with any programming language

How to write JSON?

  • Curly braces define objects in JSON
  • object members are name & value pairs
  • Name is always in double quotes

JSON values

  • Numbers: no quotes || Ex: “id”: 49
  • String: double quotes || Ex: “Name” : “name”
  • Boolean: true, false || Ex: “Yes”: true
  • Array || Ex: “List”: [1,2,3,4]
  • Nested JSON objects
  • Null

Java JSON Data Binding

  • Data binding is the process of converting /mapping /serializing /marshaling JSON data to a Java POJO and vice et versa.
  • Spring uses Jackson project for data binding behind the scenes.
  • By default, Jackson will call the appropriate getter/setter methods whenever they are called.

JSON to Java POJO

When JSON needs to be converted to Java POJO, the setter methods will be called on POJO.

Java POJO to JSON

When Java POJO needs to be converted to JSON, the getter methods will be called on POJO.

How to ignore unknown properties?

The way you ignore properties that you just added into the JSON but your program doesn’t know about is you add an annotation
@JsonIgnoreProperties(ignoreUnknown=true)

Leave a comment

Design a site like this with WordPress.com
Get started