2

I am running this line of code,

m =requests.post(api_url + 'accounts', json=order, auth=auth)
print m.json()

which produces the following output:

[{u'available': u'0.4', u'balance': u'0.5'}, {u'available': u'6.8', u'balance': u'9.0'}]

My goal is to be able to save the different "availables" and "balances" into FLOAT variables, so I can use them further in the code.

Currently, I am trying to do

output = float(m['available'])
print(output) 

But this is not working ... and I am also unsure how to separate them both, given that there is a { curly bracket} between the two "availables" and "balances"

3
  • Try m.json['available'] Commented Mar 10, 2016 at 6:10
  • ...and when posting questions, "this is not working..." is way too vague. Tell us how its not working, preferably with a stack trace when available. You are making us guess when you have the information. Commented Mar 10, 2016 at 6:12
  • Did that work? Let us know so that others don't waste their time pondering the question. Commented Mar 10, 2016 at 6:31

1 Answer 1

2

I think the json string you get looks like that

[
    {
        "available": "0.4",
        "balance": "0.5"
    },
    {
        "available": "6.8",
        "balance": "9.0"
    }
]

Which means m.json() will return a list with two dictionaries in it .

You should use float(json[0]['available']) to get the value .

Sign up to request clarification or add additional context in comments.

10 Comments

Dude, you are really kind !
I am glad to help :)
Enjoy the association bonus :grrrrr :D
thanks, but it seems to not be working... i am getting the error TypeError: 'module' object has no attribute '__getitem__' . The code I am using is r = requests.get(api_url + 'accounts', auth=auth) print r.json() m = float(json[0]['available']) print m @KIDJourney
@itsneo help if you can
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.