I have a REST API endpoint in which I need to parse incoming nested JSON of the format:
site: {
id: '37251',
site_name: 'TestSite',
address: {
'address': '1234 Blaisdell Ave',
'city': 'Minneapolis',
'state': 'MN',
'zip': '55456',
'neighborhood': 'Kingfield',
'county': 'Hennepin',
},
geolocation: {
latitude : '41.6544',
longitude : '73.3322',
accuracy: '45'
}
}
into the following SQLAlchemy classes:
Site:
class Site(db.Model):
__tablename__ = 'site'
id = Column(Integer, primary_key=True, autoincrement=True)
site_name = Column(String(80))# does site have a formal name
address_id = Column(Integer, ForeignKey('address.id'))
address = relationship("Address", backref=backref("site", uselist=False))
geoposition_id = Column(Integer, ForeignKey('geoposition.id'))
geoposition = relationship("Geoposition", backref=backref("site", uselist=False))
evaluations = relationship("Evaluation", backref="site")
site_maintainers = relationship("SiteMaintainer", backref="site")
Address (a Site has one Address):
class Address(db.Model):
__tablename__ = 'address'
id = Column(Integer, primary_key=True, autoincrement=True)
address = Column(String(80))
city = Column(String(80))
state = Column(String(2))
zip = Column(String(5))
neighborhood = Column(String(80))
county = Column(String(80))
and Geoposition (a Site has one Geoposition):
class Geoposition(db.Model):
__tablename__ = 'geoposition'
id = Column(Integer, primary_key=True, autoincrement=True)
site_id = Column(Integer)
latitude = Column(Float(20))
longitude = Column(Float(20))
accuracy = Column(Float(20))
timestamp = Column(DateTime)
Getting the SQLAlchemey data into JSON is easy, but I need to parse the JSON from my request so that I can append/update data that is sent via POST to the RESTful API. I know how to handle non-nested JSON, but I will be the first to admit that I am clueless in dealing with the nested JSON for records that belong to multiple tables in a relational structure.
I've tried searching high and low for this without any luck. Closest I could find is here "Nested validation with the flask-restful RequestParser", but this is not clicking for what I need to do based on my nested structure.