using simplejson.dumps to encode a dictionary to json

Chris Rebert clp2 at rebertia.com
Tue Mar 15 16:22:26 EDT 2011


On Tue, Mar 15, 2011 at 12:56 PM, Aaron <aaron.jerling at gmail.com> wrote:
> If I print authreq_data to screen  I get
>
> {"req": {"username": "######", "password": "#####", "productType": "CFD_Demo"}}
>
> Essentially I want the inner brackets to be  [ ] instead of {} but alternating on each level so it would be:
>
> {"req": [{"username": "######", "password": "#####", "productType": "CFD_Demo"}]}
>
> as per usual json.

I don't think there's a "usual JSON", but anyway, if you want a list,
then just use one:

from json import dumps
loginreq = {"username":username, "password":password, "productType":"CFD_Demo"}
authreq_data = {"req":[loginreq]} # note brackets
authreq_data = dumps(authreq_data)

>>> print(authreq_data)
{"req": [{"username": "USER", "password": "PASS", "productType": "CFD_Demo"}]}

Cheers,
Chris
--
http://blog.rebertia.com



More information about the Python-list mailing list