Hi,

This is the code I use to render a ROW in JSON. You also have to XML it to 
render it correctly in view.

def rows2json (tablename,rows):
    import datetime
    import json
    def date_handler(obj):
        if isinstance(obj, datetime.datetime):
            return obj.strftime(str(T('%d/%m/%Y %T')))
        elif isinstance(obj, datetime.date):
            return obj.strftime(str(T('%d/%m/%Y')))
        else:
            return False
    rows = rows.as_list()
    concat = '{ "'+tablename+'": ['
    for row in rows:
        concat = concat + json.dumps(row, default=date_handler)+","
    concat = concat.strip(',')
    concat = concat + ']}'
    return concat


PS: it also does check for dates to get them viewed in dd-mm-yyyy format.

Mike

Le dimanche 17 avril 2016 20:19:39 UTC+2, Raphael Lechner a écrit :
>
> Hi,
>
> I'm trying to build an application with ember.js as frontend and web2py as 
> backend. Ember requires a valid json:api (http://jsonapi.org/) interface.
> I tried to use the generic json view but the output is different.
>
> Example output from generic json view:
> {"data": [{
>     "name": "100",
>     "filename": 
> "pictures.picture_file.8dd3626ec0b3a290.494d475f32303135303532365f3130353432342e6a7067_print.jpg",
>     "id": 1
>   },
>   {
>     "name": "101",
>     "filename": 
> "pictures.picture_file.8e48a177244f1ea9.494d475f32303135303532365f3130353435302e6a7067_print.jpg",
>     "id": 2}
>   ]}
>
> Valid json:api output:
> {"data": [{
>     "id": "100",
>     "type": "picture",
>     "attributes": {
>       "name": "100",
>       "filename": 
> "pictures.picture_file.8dd3626ec0b3a290.494d475f32303135303532365f3130353432342e6a7067_print.jpg",
>     }
>   },
>   {
>     "id": "101",
>     "type": "picture",
>     "attributes": {
>       "name": "101",
>       "filename": 
> "pictures.picture_file.8dd3626ec0b3a290.494d475f32303135303532365f3130353432342e6a7067_print.jpg",
>     }
>   }
>   ]}
>
> Is there a way to change the schema of the json output?
>
> Thanks!
> Raphael
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to