DEV Community

Cover image for How to quickly prototype Hibernate or Django model classes from a JSON Object
Stephane Piedjou
Stephane Piedjou

Posted on

How to quickly prototype Hibernate or Django model classes from a JSON Object

Despite the plethora of libraries and frameworks I still find It time consuming, error prone and a repetitive work to write model classes for the ORMs I am using in my different projects including Django model, Hibernate, Doctrine or Room persistence library. Most of the time they have similar architecture with a set of imports, class definition, annotations, attributes, constructors and getters and setters.

This has led me to think of a way to speed up this process. I started brainstorming and then working on a tool that given a particular JSON object, would generate a code that corresponds to the ORM of interest, a sort of mapper, that maps the given JSON object to an ORM tables with its attributes and methods if applicable.

You can visit the end result here

The approach

Person object

Fig.1.1 - A person Object in JSON

The approach was quite simple. Let's consider the JSON in Fig 1.1 representing a Person with several attributes. The Person Object is converted into a table with Person as the table’s name and the key-value pairs as the table’s attributes. Below is the JSON object transformed into its Django Model class equivalent.

person_object_django

Fig.1.2 - Person Object mapped into its class equivalent in Django Model

We have several exceptions though.

  • If we have a JSON Object as an embedded object of another JSON Object as shown in Fig 1.3, the embedded JSON Object, in this case Address, will be transformed into another table. A One to One relationship will be set between the two tables which in our example are Person and Address as shown in Fig 1.4

person_id = models.OneToOneField(Person,on_delete=models.CASCADE)

person_address_json

Fig.1.3 - JSON object with embedded JSON Object

person_address_django

Fig.1.4 - One to One relationship, Django Model
  • In the case we have a JSON array as an embedded key-value pair of another JSON Object, and that the value of the JSON array are JSON Object as shown in Fig 1.5, the first element of the array is considered and will be transformed into a table, in this case visited cities. A Many to One relationship will be set between the two tables, in our case Person table and the Visited cities table. Fig 1.6 shows the output for the conversion into Django Model

person_id = models.ForeignKey(Person, on_delete=models.CASCADE)

person_list_cities_json

Fig.1.5 - A JSON Array embedded in a JSON Object

person_list_cities_django

Fig.1.6 - Django Model equivalent of Fig 1.5
  • The last exception is when the embedded JSON array is a list of simple data types such as strings, numbers or boolean. The first element will be used to determine the type of that attribute.
    person_list_string_json
    Fig.1.7 - A person with a list of hobbies
    person_list_string_django
    Fig.1.8 - The list of hobbies is converted as Character field

Supported ORMs

So far the tool has basic support for Django Model and Hibernate and can also generate SQL create statements for PostgreSQL database. The plan in the near future is to support Doctrine and Room persistence library and be able to generate SQL create statements for MySQL database.

Attribute types

To identify the attribute type to use in our models, the JSON value types are mapped into their corresponding SQL types. We therefore have

  • Boolean: “false” or false in the JSON value is converted into a Boolean;
  • Numbers: “23” or 23 in the JSON value is converted into an Integer;
  • Date: Will mapped string in a format “MM-dd-yyyy” into a Date;
  • String: if a field is neither a Boolean nor a Number it will be mapped into a String field.

Limitations

  • Field types: The tool is quite limited to a few attribute types at the moment. Knowing that more fields types are supported by ORMs we tool can evolve to take them into consideration.
  • Relationships: The same thing applies to relationship between tables. So far only One To One and Many to One relationships are available
  • Other configuration: Cascade deletion is used by default with One To One and Many To One relationships while there are not always the best choices. The tool should somehow allow users to determine which configuration they want to use.

Next steps

  • Take into consideration more attribute types, relationships and configuration
  • Generate fixtures using the data passed in the JSON Object

Contribution

You can head to the GitHub repository if you want to contribute to the project or use the JSON to ORM mapper online tool.
Thank you for reading till the end, I hope you enjoyed it.

Top comments (0)