# How to use json datatype in Hibernate using SqlServer

**URL:** <https://discourse.hibernate.org/t/how-to-use-json-datatype-in-hibernate-using-sqlserver/437>\
**Category:** Hibernate ORM\
**Created:** [March 27, 2018, 12:34pm UTC](https://discourse.hibernate.org/t/how-to-use-json-datatype-in-hibernate-using-sqlserver/437 "2018-03-27T12:34:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Swamy\_Rapolu](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Swamy\_Rapolu](https://discourse.hibernate.org/u/Swamy_Rapolu)\
**Post date:** [March 27, 2018, 12:34pm UTC](https://discourse.hibernate.org/t/how-to-use-json-datatype-in-hibernate-using-sqlserver/437/1 "2018-03-27T12:34:44Z")

</div>

Hi,

I am new to hibernate and so far everything seems working fine. I have a json which has 4 values. Out of 4, 3 are normal string values. But in the 4th value there is a complete json data which I need to store as it is in the SqlServer.

> @JsonProperty(“DETAIL”)  
> @Column(name = “DETAIL”)  
> private String jsonCheck;

Also I am using spring boot for logic purposes.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [March 27, 2018, 1:05pm UTC](https://discourse.hibernate.org/t/how-to-use-json-datatype-in-hibernate-using-sqlserver/437/2 "2018-03-27T13:05:40Z")

</div>

By default, Hibernate does not support JSON. However, you can use this [hibernate-types](https://github.com/vladmihalcea/hibernate-types) open source project which provides additional types for JSON or ARRAYs.

But this is only useful if you want to use a JSON column type. if you use a VARCHAR or a TEXT column type, then you can just map the JSON as a String, as you already did:

```auto
@Column(name = “DETAIL”)
private String jsonCheck;

```

---

<div class="post-metadata">

**Author:** ![Swamy\_Rapolu](https://avatars.discourse-cdn.com/v4/letter/s/b5e925/32.png) [@Swamy\_Rapolu](https://discourse.hibernate.org/u/Swamy_Rapolu)\
**Post date:** [March 27, 2018, 1:22pm UTC](https://discourse.hibernate.org/t/how-to-use-json-datatype-in-hibernate-using-sqlserver/437/3 "2018-03-27T13:22:54Z")

</div>

Hi Vlad,

I have checked your posts and blogs in stackoverflow. You have mentioned in your blog that open project is supported for postgreSql & MySql. I wanted to ask you whether the open source project is compatible to SqlServer also.

---

<div class="post-metadata">

**Author:** ![vlad](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/vlad/32/658_2.png) [@vlad](https://discourse.hibernate.org/u/vlad)\
**Post date:** [October 15, 2019, 11:03am UTC](https://discourse.hibernate.org/t/how-to-use-json-datatype-in-hibernate-using-sqlserver/437/5 "2019-10-15T11:03:02Z")

</div>

I have tested it and it works like a charm.

Assuming this is your database table:

```auto
CREATE TABLE book (
    id BIGINT NOT NULL PRIMARY KEY,
    isbn VARCHAR(15),
    properties NVARCHAR(4000) CHECK(
        ISJSON(properties) = 1
    )
)

```

you can map it the JPA entity like this:

```auto
@Entity(name = "Book")
@Table(name = "book")
@TypeDef(
    name = "json",
    typeClass = JsonStringType.class
)
public class Book {
 
    @Id
    private Long id;
 
    @NaturalId
    @Column(length = 15)
    private String isbn;
 
    @Type(type = "json")
    private String properties;
 
    public Long getId() {
        return id;
    }
 
    public Book setId(Long id) {
        this.id = id;
        return this;
    }
 
    public String getIsbn() {
        return isbn;
    }
 
    public Book setIsbn(String isbn) {
        this.isbn = isbn;
        return this;
    }
 
    public String getProperties() {
        return properties;
    }
 
    public Book setProperties(String properties) {
        this.properties = properties;
        return this;
    }
 
    public JsonNode getJsonNodeProperties() {
        return JacksonUtil.toJsonNode(properties);
    }
}

```

Check out [this article](https://vladmihalcea.com/sql-server-json-hibernate/) for more details.
