RigidBody
RigidBody

Reputation: 664

GAE python27 return nested json

This seems such a simple task, yet it eludes me...

class ViewAllDogs(webapp2.RequestHandler):
    """ Returns an array of json objects representing all dogs. """
    def get(self):
        query = Dog.query()
        results = query.fetch(limit = MAX_DOGS)   # 100
        aList = []
        for match in results:
            aList.append({'id': match.id, 'name': match.name,
                           'owner': match.owner, arrival_date':match.arrival_date})
            aList.append({'departure_history':{'departure_date': match.departure_date,
                          'departed_dog': match.departed_dog}})
        self.response.headers['Content-Type'] = 'application/json'
        self.response.write(json.dumps(aList))

The above, my best attempt to date, gets me:

[
  {
    "arrival_date": null,
    "id": "a link to self",
    "owner": 354773,
    "name": "Rover"
  },
  {
    "departure_history": {
      "departed_dog": "Jake",
      "departure_date": 04/24/2017
    }
  },

 # json array of objects continues...
]

What I'm trying to get is the departure_history nested:

[
  {
    "id": "a link to self...",
    "owner": 354773,
    "name": "Rover",
    "departure_history": {
      "departed_dog": "Jake",
      "departure_date": 04/24/2017
      },
    "arrival_date": 04/25/2017,
  },

# json array of objects continues...
]

I've tried a bunch of different combinations, looked at json docs, python27 docs, no joy, and burned about way too many hours with this. I got this far with the many related SO posts on this topic. Thanks in advance.

Upvotes: 0

Views: 24

Answers (2)

minou
minou

Reputation: 16573

You can simplify a little:

        aList = []
        for match in results:
            aDog = {'id': match.id, 
                    'name': match.name, 
                    'owner': match.owner, 
                    'arrival_date':match.arrival_date,
                    'departure_history': {
                        'departure_date': match.departure_date,
                        'departed_dog': match.departed_dog}
                   }
            aList.append(aDog)

Upvotes: 2

RigidBody
RigidBody

Reputation: 664

This seems a bit hackish, but it works. If you know a better way, by all means, let me know. Thanks.

 class ViewAllDogs(webapp2.RequestHandler):
        """ Returns an array of json objects representing all dogs. """
        def get(self):
            query = Dog.query()
            results = query.fetch(limit = MAX_DOGS)   # 100
            aList = []
            i = 0
            for match in results:
                aList.append({'id': match.id, 'name': match.name,
                               'owner': match.owner, arrival_date':match.arrival_date})
                aList[i]['departure_history'] = ({'departure_history':{'departure_date': match.departure_date,
                              'departed_dog': match.departed_dog}})
             i += 1
            self.response.headers['Content-Type'] = 'application/json'
            self.response.write(json.dumps(aList))

Upvotes: 0

Related Questions