[sqlalchemy] Re: sqlalchemy and json
alex wrote: > ok, and what about {1:{"id":"1","name":"Alex"},2: > {"id":"2","name":"Nick"}} format? > > I try to > i=0 > result = list() > for row in session.query(...).all(): > d = row.__dict__.copy() > del d['_sa_instance_state'] > result.append(i) > result.append(d) > i = i + 1 > > but it gives > { 1, {"id":"1","name":"Alex"}, 2, {"id":"2","name":"Nick"}} It's giving you just what you ask for: for every row, append two things to the list: an integer followed by a dictionary for the row. (I'm dubious about the outer curly braces, though; when I try a similar chunk of code, I get "[...]".) If you want a dictionary whose keys are the values of "i", define "result" initially as "result = {}", then in the loop, replace the appends by "result[i] = d". If you just want a list of the row dictionaries, drop all the statements involving "i". -- Don Dwiggins Advanced Publishing Technology --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: sqlalchemy and json
ok, and what about {1:{"id":"1","name":"Alex"},2: {"id":"2","name":"Nick"}} format? I try to i=0 result = list() for row in session.query(...).all(): d = row.__dict__.copy() del d['_sa_instance_state'] result.append(i) result.append(d) i = i + 1 but it gives { 1, {"id":"1","name":"Alex"}, 2, {"id":"2","name":"Nick"}} --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: sqlalchemy and json
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 25.03.2009 7:17 Uhr, alex wrote: > sorry, but I need numeric of dicts {1:{"id":"1","name":"Alex"},2: > {"id":"2","name":"Nick"}} to use this in ajax-call JSON does not know about date-ish types. You have to convert such fields explicitly e.g. to a timestamp - means you need to iterate over the instance dict and check the types of the values and convert them accordingly. - -aj -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAknKzXAACgkQCJIWIbr9KYwUzwCeNYfjf7CfnArX8iYRCCKjVESv H48AnikvpBz+RQjmuJq49JEIA5k+T9Gx =4sYk -END PGP SIGNATURE- --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~--- begin:vcard fn:Andreas Jung n:Jung;Andreas org:ZOPYX Ltd. & Co. KG adr;quoted-printable:;;Charlottenstr. 37/1;T=C3=BCbingen;;72070;Germany email;internet:i...@zopyx.com title:CEO tel;work:+49-7071-793376 tel;fax:+49-7071-7936840 tel;home:+49-7071-793257 x-mozilla-html:FALSE url:www.zopyx.com version:2.1 end:vcard
[sqlalchemy] Re: sqlalchemy and json
sorry, but I need numeric of dicts {1:{"id":"1","name":"Alex"},2: {"id":"2","name":"Nick"}} to use this in ajax-call --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: sqlalchemy and json
Thanks, it gets results. But I have an error "TypeError: datetime.datetime(2009, 3, 11, 9, 56, 6) is not JSON serializable" --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: sqlalchemy and json
It gets result - first row. But I have an error "TypeError: datetime.datetime(2009, 3, 11, 9, 56, 6) is not JSON serializable" --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~---
[sqlalchemy] Re: sqlalchemy and json
-BEGIN PGP SIGNED MESSAGE- Hash: SHA1 On 24.03.2009 13:45 Uhr, alex wrote: > Hello. > > I need to get query from sqlalchemy in json format. > > query like > query_name = session.query(model.Users); > > How to format like > {{"id":"1","name":"Alex"},{"id":"2","name":"Nick"}.} The result format does not make much sense (dict of dicts). You want a list of dicts. Something like that might work result = list() for row in session.query(...).all(): d = row.__dict__.copy() del d['_sa_instance_state'] result.append(d) - -aj -BEGIN PGP SIGNATURE- Version: GnuPG v1.4.9 (Darwin) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ iEYEARECAAYFAknI7S0ACgkQCJIWIbr9KYxTOgCgkz4t1xTmu/5/7ND20SPfak5f vrMAoJWtzssD7TnTxkSTfmnPYmYwqWJQ =sdOD -END PGP SIGNATURE- --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~--~~~~--~~--~--~--- begin:vcard fn:Andreas Jung n:Jung;Andreas org:ZOPYX Ltd. & Co. KG adr;quoted-printable:;;Charlottenstr. 37/1;T=C3=BCbingen;;72070;Germany email;internet:i...@zopyx.com title:CEO tel;work:+49-7071-793376 tel;fax:+49-7071-7936840 tel;home:+49-7071-793257 x-mozilla-html:FALSE url:www.zopyx.com version:2.1 end:vcard