Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to store json object in a column of mysql using spring boot/java

Requirement is like that a json object is send from postman and i need to receive it on a restcontroller and save that whole json object as it is to the mysql database Table Column(in one/single column) using spring boot.

{
    "name" : "abc",
    "address : "xyz"
}

assume the above json object is send from postman to a spring boot restcontroller. How can i save that json object in a column of a table .

like image 331
Surjeet Singh Rathore Avatar asked Nov 15 '25 22:11

Surjeet Singh Rathore


2 Answers

MySQL supports JSON column types. https://dev.mysql.com/doc/refman/8.0/en/json.html

So, assuming you will be using json column type for your requirement, you can follow below articles. I was able to do this for postgres as well, works very well for me, the instructions are pretty straightforward.

https://vladmihalcea.com/how-to-map-json-objects-using-generic-hibernate-types/ https://vladmihalcea.com/java-map-json-jpa-hibernate/

like image 191
vb.stack Avatar answered Nov 18 '25 13:11

vb.stack


There's no way. You can save it parsing the JSON to String ( Ex: JSON.stringify() ) and when you query the info , parse the string to JSON (Ex: JSON.parse() )

JSON.stringify: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/JSON/stringify

JSON.parse: https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/JSON/parse

like image 42
David Dionis Avatar answered Nov 18 '25 13:11

David Dionis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!