JSON, which stands for JavaScript Object Notation, is a lightweight text-based data format that is easy for humans to read and write and for machines to parse and generate. It is commonly used for transmitting data or data exchange in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).
Table of contents
1. JSON Data Format
Below are some of the standard JSON structures:
Basic JSON Strings
This is how a JSON message look likes:
{
"name": "mkyong"
}
JSON Object
JSON can contains strings, numbers, arrays, booleans, and other object literals.
{
"name": "mkyong",
"age": 42
"skills": ["react", "python"],
"active": true
}
JSON Array
Array of JSON objects.
[
{
"name": "mkyong",
"age": 20
},
{
"name": "ah pig",
"age": 40
},
{
"name": "ah dog",
"age": 30
}
]
2. Java JSON Libraries
List of the Java JSON libraries
2.1 Jackson
Jackson is a high-performance JSON processor for Java, known as the "Java JSON library", the most popular JSON framework in Java, picks this if you are not sure which to pick.
- Parse JSON string with Jackson
- Parse JSON Array with Jackson
- Convert JSON string to Map using Jackson
- Convert Java Objects to JSON with Jackson
- How to ignore a field with Jackson
- How to ignore null fields with Jackson
- How to map a subset of a JSON file using Jackson
- How to enable pretty print JSON with Jackson
- Write JSON to a file with Jackson
- Jackson @JsonView examples
- Jackson and Lombok examples
- Jackson Streaming API examples
- Jackson supports Java 8 date/time api
- Jackson Tree Model examples
- Jackson custom field name with @JsonProperty
- Jackson Custom Serializer and Deserializer Examples
P.S. All Jackson examples are tested with Jackson 2.17.0.
2.2 Google Gson
Gson is a Java library used to convert Java Objects into their JSON representation and vice versa.
- How to parse JSON using Gson
- How to parse JSON Array using Gson
- How to pretty print JSON using Gson
- Read and Write JSON to File using Gson
- Convert Java objects to from JSON using Gson
- Gson Custom field name with @SerializedName
- Gson Streaming APIs examples
- Gson supports Java 8 date time types
- How to exclude fields in Gson
P.S. All Gson examples are tested with Jackson 2.10.1.
2.3 Moshi
Moshi is a modern JSON library for Android, Java and Kotlin. It makes it easy to parse JSON into Java and Kotlin
- Parse JSON using Moshi
- Write JSON to a file using Moshi
- Pretty print JSON using Moshi
- Moshi supports Java 8 date time APIs
- Moshi custom adapter for java.math.BigDecimal
P.S. All Moshi examples are tested with Moshi 1.15.1.
2.4 JSON.simple
The JSON.simple library is a lightweight utility for deserializing and serializing Javascript Object Notation (JSON).
P.S. All JSON.simple examples are tested with json-simple 4.0.1.
2.5 FastJson
Tested with FastJson 1.2.57 (Need Update)
3. FAQs
- How to Pretty Print JSON output with cURL
- JSONAssert – How to unit test JSON data
- Spring Test – How to test a JSON Array in jsonPath
- JavaScript Array to JSON
4. Download Source Code
$ git clone https://github.com/mkyong/java-json
$ cd gson