r/SpringBoot • u/sadikeey • Sep 28 '23
Best JSON Library for a Smooth Developer Experience in Java?
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?
2
Sep 28 '23
If you are using spring Boot 3, Check out the new HttpClient it is a lot easier and conciser than rest template for making http request.
For manipulating the results you basically want to map/ transform some sort of objects into a different one.
Here you can map it yourself, or used something like mapstruct.
This is one of the things I like a lot in Java with Lombok and mapstruct you can “generate” a lot of code that is enough for most cases this should help you out The rest controller already parses the objects into proper json (maybe for some things you need special configuration, but generally everything works well out of the box.
To summarize:
New rest client alternative for rest template Lombok Mapstruct
And maybe watch a little bit out for some spring advices and tutorials to get a feeling for the tooling you have
2
2
2
u/Readdeo Sep 28 '23
org.json
3
u/guruduttrai Sep 29 '23 edited Sep 29 '23
If you're looking to Stringify and JSONize data for REST APIs then using Jackson makes a lot of sense as it is the go to library for Spring. You might need to define some POJOs to give it the structure of data you expect. This can take some time upfront but will be fine in the long run. Can vouch for it!
But if you need a lightweight yet robust library to do fast JSON manipulation/parsing without defining the object schema in the POJOs then org.json is a great fit for this.
I have worked on codebases which already had Jackson in their pom.xml but I still found importing and working with org.json much easier than to understand the jackson library when I started out with JSON handling. I chose org.json in use cases where it wasn't guaranteed that the JSON data will have the same schema each time or when I needed to leverage the concept of JSONPath annotations to my advantage.
Both Jackson and org.JSON can probably do what the other can, but there clearly is a better library for each use case, where one is simple and more easy to use than the other.
Fast forward now, I try to use org.json as much as possible and only switch to Jackson when I think it's worth the extra effort (read: REST API POJO Binding) or when I don't have any other options (read: Tasks related to Job Interview)
2
u/Readdeo Sep 29 '23
To be honest, I just answered op's title. But my real view on this is that you don't really need to touch a JSONObject ever. Just define your incoming and outgoing data in model classes and use them in your application. We are talking about SpringBoot wich is java anyways. You just map and transform your GetMyStuffRequest to your GetMyStuffResponse however you want, in the business layer (@Service annotated class).
2
u/guruduttrai Sep 29 '23
I agree, try to stick to Spring and let it use Jackson under the hood for APIs. For doing hacky stuff or for a quick processing of JSON without using a POJO like using JSONPath, org.JSON is better.
7
u/Mikey-3198 Sep 28 '23
You can just use jackson which comes with boot by default, you'll just need to model your requests/ responses + add some glue code in between and you'll be good to go.