Store JSON in PostgreSQL with Spring Boot

In modern applications, dealing with semi-structured or flexible data formats has become increasingly common. PostgreSQL offers robust support for JSON and JSONB columns, allowing developers to store, query, and index JSON data efficiently.

In this blog post, we'll explore how to store JSON data in PostgreSQL using Spring Boot and Hibernate. You'll learn how to map JSON fields to a Java object and persist them in the database. Whether you're working with dynamic configurations, logs, or embedded documents, PostgreSQL's JSON capabilities can simplify your data modeling and querying.

For the purposes of this blog post, we’ll use the sql-springboot-basics-demo project as a foundation. On top of it, we’ll demonstrate how to use a JSON field in PostgreSQL and map it properly using Spring Boot and Hibernate.

Moving from Theory to Practice

Now that we've covered the basics of JSON support in PostgreSQL and how it can be useful for storing flexible or nested data structures, it's time to put it into practice using a real Spring Boot project.

  1. Defining the Helper Model: ContactDetails

Before we can store JSON data in our PostgreSQL database, we need to define a simple helper class that will be used as the structure for our JSON field. In our case, we’ll use a class named ContactDetails to represent various contact-related information such as phone number, email, and address.

Article content

2. Expanding the PersonModel Entity

Now that we've defined the ContactDetails helper model, we can extend our PersonModel entity to include a JSON field. We'll use the @JdbcTypeCode annotation from Hibernate to tell it how to handle the Java type, and we'll specify the column type in PostgreSQL using columnDefinition.

Article content

Why @JdbcTypeCode(SqlTypes.JSON) and columnDefinition = "jsonb"?

  • @JdbcTypeCode(SqlTypes.JSON) tells Hibernate how to convert the Java ContactDetails object into a format suitable for storing in the database. Internally, Hibernate will serialize the object as JSON when persisting it and deserialize it when reading.
  • @Column(columnDefinition = "jsonb") explicitly tells PostgreSQL to use the jsonb column type instead of the default text or other types. This enables PostgreSQL to store the JSON data in a binary format, allowing for efficient indexing and querying.

3. Refactor PersonService and use contactDetails field:

Article content

4. Add the ContactDetailsTO Transfer Object

Article content

5. Extend PersonTO with ContactDetailsTO

Article content

6. Add the ContactDetails Mapper

Article content

7. Update Post and Put HTTP methods (create and update) in PersonController:

Article content

Storing JSON data directly in PostgreSQL allows for flexible and efficient management of complex nested objects within relational databases. By leveraging Spring Boot’s support for JSONB columns and simple entity mapping, we can seamlessly integrate JSON handling into our applications without sacrificing type safety or query performance.

You can find the complete example project here: springboot-postgres-json-demo


To view or add a comment, sign in

More articles by Marko Zivkovic

Others also viewed

Explore content categories