Hi Devs, I'm relatively new to the world of backend development, and I'm currently working on a Java project using the Spring Boot framework. My application involves a lot of JSON data manipulation. Here's what I'm up to:
I fetch data from some APIs, manipulate it according to my application's needs, and provide that data through controllers. JSON plays a central role in this process.
As a beginner in this field, I'm looking for advice from experienced developers. Which JSON library have you found to offer the best developer experience when working in Java, especially within a Spring Boot project?
In my case, what factors should I prioritize for a smooth development experience considering the heavy JSON manipulation I'm doing?
Videos
I have knowledge of Java, and I have made many programs with this language. But now, I want to learn how to work with JSON files. I use Maven and have heard that there are many libraries for JSON. What is the best and easiest one?
The two most popular and fully-featured Java JSON libraries according to Maven Repository are:
- GSON (2.4)
- Jackson (2.6)
and jars for both are accessible via Maven:
- Gson requires
gson-2.4.jar(group idcom.google.code.gson, artifact idgson - Jackson requires
jackson-databind-2.6.2.jar(group idcom.fasterxml.jackson.core, artifactjackson-databind), as well as 2 supporting jars (jackson-corefor streaming parser,jackson-annotationsfor annotation suport)
Jars that you listed are for other lesser commonly used packages (one for json-lib, http://json-lib.sourceforge.net/ is pretty old; other I don't even know what it is), so I would not recommend you use them.
The missing class in the NoClassDefFoundError is located in commons-lang.jar from apache.
It can be downloaded here.
Minimal, standard-first JSON writer and parser. One single Java file, 1k LoC, no dependencies.
Background
I often write small Java tools (CLI, Gradle plugins, scripts) that need to read/write JSON. I really don't want to use JSON libraries that larger than my codebase, so I wrote json4j.
Usage
You can use as dependency:
implementation("io.github.danielliu1123:json4j:+")or use as source code, just copy Json.java into your codebase:
mkdir -p json && curl -L -o json/Json.java https://raw.githubusercontent.com/DanielLiu1123/json4j/refs/heads/main/json4j/src/main/java/json/Json.java
There are only two APIs:
record Point(int x, int y) {}
// 1) Write JSON
Point point = new Point(1, 2);
String json = Json.stringify(point);
// -> {"x":1,"y":2}
// 2) Read JSON
// 2.1) Simple type
String json = "{\"x\":1,\"y\":2}";
Point point = Json.parse(jsonString, Point.class);
// -> Point{x=1, y=2}
// 2.2) Generic type
String json = "[{\"x\":1,\"y\":2},{\"x\":3,\"y\":4}]";
List<Point> points = Json.parse(jsonString, new Json.Type<List<Point>>() {});
// -> [Point{x=1, y=2}, Point{x=3, y=4}]That's all!
Link
Repo: https://github.com/DanielLiu1123/json4j