How to convert json into a database with assocations

Hello, 


Suppose I have these two tables 


Person                                     

ID                                                                                
address                                  
city
role :  Director 


PersonRole :  

Id
PersonId
roleID 


Role:

Id 

Description : 


and now I have a json which c


an look like this 

{  Person :
  {  name :   a,
     address : b
     city       : c
     role : Director } ,
    Person : {
      name : a1
      address:  b2
      city :  c2
      role: Director
    }


what is now the best way to take care that everything will be placed in the database but the associatoons must be right. So every role must be only once in the database. The same as every Person. 


Regards,, 


Roelof


 
 

Solution

Hi Roelof,

to take care of your JSON you need to make a structure with the same attibuts as your JSON.

I think you may use a solution on local variables on a server action where you append all the response of your JSON, then with for each check the role that came as a "string" check with a swith to make select de value you need on your data base and for each record check if the combination of "ROLE" and the atributs of "PERSON" on a Aggregate then use a IF to check if that query return you "empty" or "not empty" to make sure that that record (current record iterated) already exist on your DB, then creat or update your record, end the for each.

Sound like a little too much work but i think that will work.

Best regarts.

Frederick Lozano.

Solution

oke, I tried to make things only work with the json responses but then some people said to store it in a database and then I can use aggegrates to display things on the homepage and do things like sorting and searching.

Roelof Wobben wrote:

oke, I tried to make things only work with the json responses but then some people said to store it in a database and then I can use aggegrates to display things on the homepage and do things like sorting and searching.

Hi Roelof, 

well thats in case you store things then made every single association by hand and then delete those temporary data to left your new date with the association.

The solution a gave to you its just store in memory on local variables just not to store things and then change it one by one, i meant that could work but i think that you can do that by code it's a little bit of workarround but at the end it work.

Regarts,

Frederick


Hi,

1. Create a MovieRole entity.

2. Create a Person entity with MovieRoleId with data type MovieRole identifier to create relationship between the entities.

Create and action that does the following:

3. Loop through your structure of the rest response

4. Per person check if the role is already in the entity MovieRole using an aggregate and then use MovieRoleCreate to insert it only when not already in the database.

5. Then check if the person is already defined and use PersonCreateOrUodate to upsert the person data, make sure to set the MovieRoleId to the one you retrieved or inserted.

Regards,

Daniel




Thanks, 


so if I understand you well, I have to enter the data PersonRole manually or does the PersonCreateOrUpdate  take care of this ? 


Roelof