# Create a JSON dataset, with dealer objects at the top, whics contains
# a list of offer objects, which include all the data in the dealer database
# as well as all product reviews from the product database.

from nested import product_db, dealer_db
import json

res = [

  select {"dealer": name,
          "offers": [

            select {"make": o.make,
                    "model": o.model,
                    "year" : o.year,
                    "reviews": p["reviews"] }

            for o in d, p in product_db

            let p_make = try p['manufacturer']['company'] except p['manufacturer']
            where o.make == p_make and o.model == p['model']

			] }

  for d in dealer_db

  group by d.dealer as name

]

print(json.dumps(res,indent=4))

play
# Create a JSON dataset, with dealer objects at the top, whics contains
# a list of offer objects, which include all the data in the dealer database
# as well as all product reviews from the product database.

from nested import product_db, dealer_db
import json

res = [

  select {"dealer": name,
          "offers": [

            select {"make": o.make,
                    "model": o.model,
                    "year" : o.year,
                    "reviews": p["reviews"] }

            for o in d, p in product_db

            let p_make = try p['manufacturer']['company'] except p['manufacturer']
            where o.make == p_make and o.model == p['model']

			] }

  for d in dealer_db

  group by d.dealer as name

]

print(json.dumps(res,indent=4))

play