title: how to create a json object from tree data structure in database?
body: <p>I'm using flask with the following model:</p>
  
  <pre><code>class NewsCategory(db.Model):
      __tablename__ = 'news_category'
      id = db.Column(db.Integer, primary_key=True)
      title = db.Column(db.String(64))
      parent_id = db.Column(db.Integer, db.ForeignKey('news_category.id'))
      children = db.relationship("NewsCategory")
  </code></pre>
  
  <p>And I want to create a json object from this model for use in navigation menu.</p>
  
  <p>I would like to parse it recursively and build a hierarchical JSON object that looks something like this:</p>
  
  <pre><code>tree = [{"title": "Node 1", "id": "1"},
           {"title": "Folder 2", "id": "2", "folder": "true", "children": [
              {"title": "Node 2.1", "id": "3"},
              {"title": "Node 2.2", "id": "4"}
            ]}
          ]
  </code></pre>