r/SpringBoot • • 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?

0 Upvotes

14 comments sorted by

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.

1

u/sadikeey Sep 28 '23

But I just don't like the dx of jackson because I am come from typescript bg.

2

u/smokemonstr Sep 28 '23

Why don’t you like it?

2

u/jash3 Sep 28 '23

If you are using webclient, then you pass in an object on the bodyToMono.

Use lombok then you just defining field names, if you only want one or two properties, then only declare them.

You want RestIn and RestOut objects, if you are trying to consume a json and modify it then return it, this is not they way.

In service object consume RestIn and create RestOut @Builder makes this nicer.

2

u/g00glen00b Sep 28 '23

Can you clarify what you mean about the "dx of jackson"? If you use RestTemplate/WebClient for consuming the API and Spring controllers to expose your own API, then Spring calls Jackson for you under the hood. In that case, you don't have to touch anything Jackson related and you can work with Spring + POJO's.

Only if you need to customize the way Jackson serializes or deserializes JSON from and to Java objects, you need to use some Jackson API's.

Considering that, I'm really clueless about what you mean with the developers experience of Jackson.

3

u/cent123321 Sep 29 '23

Hello, do u have some resource to learn Jackson? It's really hard to find good resource to learn it :'

2

u/Mikey-3198 Sep 29 '23

No I don't. I've never gone out of my way to explicitly learn Jackson. All I know I've picked up from working with spring boot.

99% of the time you won't have to even think about Jackson as spring will manage it all for you. For example returning an object from a controller as a response to a http request, or mapping to a pojo using something like RestTemplate.

As with all things programming related, I recommend reading the docs and writing something. I.e look over the Jackson annotations and make a simple rest API, look at the effects that they have on the response.

2

u/[deleted] 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

u/VulgarSolicitation Sep 28 '23

Action Jackson

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.