Assigning value to the JSON attribute

Usually, a JSON structure contains a combination of dictionaries and lists containing simple types, such as numbers, strings and boolean. Let’s create a couple of objects with a simple JSON structure:

  1. p1 = Product(name='Samsung Galaxy S7 edge',
  2. info={
  3. 'display': {
  4. 'size': 5.5,
  5. },
  6. 'battery': 3600,
  7. '3.5mm jack': True,
  8. 'SD card slot': True,
  9. 'colors': ['Black', 'Grey', 'Gold'],
  10. },
  11. tags=['Smartphone', 'Samsung'])
  12. p2 = Product(name='iPhone 6s',
  13. info={
  14. 'display': {
  15. 'size': 4.7,
  16. 'resolution': [750, 1334],
  17. 'multi-touch': True,
  18. },
  19. 'battery': 1810,
  20. '3.5mm jack': True,
  21. 'colors': ['Silver', 'Gold', 'Space Gray', 'Rose Gold'],
  22. },
  23. tags=['Smartphone', 'Apple', 'Retina'])

In Python code a JSON structure is represented with the help of the standard Python dict and list. In our example, the info attribute is assigned with a dict. The tags attribute keeps a list of tags. These attributes will be serialized to JSON and stored in the database on commit.