简体   繁体   中英

Creating Postgres table based on nested JSON?

is there a better way to create DB table for the particular JSON data I'm trying to work with?

I'm making a basic CRUD REST app with Spring/Hibernate that handles JSON data that I got from https://jsonplaceholder.typicode.com/users

As you can see in the link, the JSON data has nested JSONs and I made them into individual Java classes named Address, Company, Geo, and User.

At this point, I'm thinking of assigning each variable in those classes into each columns of the table by using @Column annotation because I don't know how to insert nested JSON or Arrays into postgres table.

So, by result, my SQL script ends up looking rather... primitive:

create table users."user"
(
    id serial
        constraint user_pk
            primary key,
    name varchar default null,
    username varchar default null,
    email varchar default null,
    street varchar default null,
    suite varchar default null,
    city varchar default null,
    zipcode varchar default null,
    lat varchar default null,
    lng varchar default null,
    phone varchar default null,
    website varchar default null,
    company_name varchar default null,
    catch_phrase varchar default null,
    bs varchar default null
);

Is there a more elegant way of creating DB table for this particular JSON data?

If you are using an ORM library like hibernate, which spring supports very well, you can look into this post . Hibernate can create a database structure based on your data entities automatically.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM