1

I am very new to python and attempting to parse through what appears to be JSON data.

import requests
import json

URL = "http://myip/cgi-bin/lun/luci/;stok=/login?form=login"

payload = {
'operation': 'login',
'username': 'someuser',
'password': 'somepassword'
}

session = requests.session()
r = requests.post(URL, data=payload)

print (r.json())

What I get back is:

{u'data': {u'stok': u'0c2f16c954cd2cafccfaf1bf50000ac6'}, u'success': True}

I need to use the random collection of letters/numbers in the single quote 0c2f16c954cd2cafccfaf1bf50000ac6 (it changes every time). I dont see any double quotes in this response. So a few questions:

  1. I dont see any double quotes in the response. Is this still json?
  2. What would be the best way to assign that middle part of random characters to a variable so I can use it in a request url I need to build next?
3
  • not pretty sure but isn't it return a dict instance? (decoded json) Commented Nov 24, 2018 at 5:33
  • 1
    The json() method does not return JSON as the name would imply. It returns the parsed JSON data already. Commented Nov 24, 2018 at 5:36
  • And just a hint: If you start learning Python, start with Python 3, version 2 is shortly before retirement. Commented Nov 24, 2018 at 5:38

1 Answer 1

3

I dont see any double quotes in the response. Is this still json?

It was JSON. r.json() deserialized the JSON response to a Python dict, and then print printed that dict.

What would be the best way to assign that middle part of random characters to a variable so I can use it in a request url I need to build next?

a_variable = r.json()["data"]["stok"]
Sign up to request clarification or add additional context in comments.

Comments

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.