#๐Ÿ”’ JSON in Bytes refuses to be extracted

23 messages ยท Page 1 of 1 (latest)

left lance
#

Hello,

I've tried like 5 different libraries to try and get this token out of a requests response and nothing will work. I've tried json, ast, BytesIO and nothing works. I just want it to go into a dataframe.

request.text = '{"token":"0Mkk2clkBQTzLSXbQK6MBmYrJtoVzJiaIN4W7JJgwEBG4BPMjKaIMB1gyVDYT3zw4_nsgS0GZJsAvorIgPKxRQQAMjgk7-ycFwcc75vuI4rVEYJk7hoYBy2dDCnIl-TMyb4uMJI_a9TiQTZgvcWlOurGJ2DeB2T0pLmujsJOSqZBbwi7TCBCDiA6P9ZzR8wanT0qaAW4HG4-mhRs1QGwiQ..","expires":1719884049865,"ssl":true}'
mortal shardBOT
#

@left lance

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

heady bolt
#

Can you show some code of things you have tried? This looks like JSON.loads is all you need

left lance
#

I tried using json loads in about 3 different ways and nothing worked so let me come up with another thing that doesnt work

heady bolt
#

"doesn't work" how? What type of error / issue are you running into?

left lance
#
import pandas as pd
import requests
import json

token_data = {
    'username': username,
    'password': password,
    'client': 'referer',
    'referer': 'https://wpdhsgisl06.dhs.sdc.pvt/server/admin',
    'f': 'json'
}

token_url = base_url + '/arcgis/sharing/rest/generateToken'
request_bytes = requests.post(token_url, data=token_data, verify=False).content
j = json.loads(request_bytes.decode('utf-8'))
df = pd.DataFrame.from_dict(j)
print(df)
#

gives me a value error "ValueError: If using all scalar values, you must pass an index"

#

so I was told to add typ='series' to the data frame and then it pulls it in as a series but then I cant do anything with it because the entire json is in one cell

left lance
heady bolt
#

Well, can you print j? What does it look like?

left lance
#

it a normal dictionary
{'token': 'SSgWqqjQkiltstJNH34dS2GcDjDGuvwwSaQM42R_pH7rWg3vqPSwkTdRcVUZ2XIKTe5__mYxA42UbNkOmQYkdXhYUVpdMGlBJOwlPjpZJjyqZZtZFWRX57O8i0suXLuzflC10f1d5DkPzHWS_TV05Ex69UTVMMeyUvMRGow1kJ6aq2rV5H86CGYUUyBzTXRP0oUy0JsJh03LpsYPppnH8w..', 'expires': 1719884516790, 'ssl': True}

#

When I do from_dict i get the same error

heady bolt
#

Well, if you want a single row, you can try df = pd.DataFrame.from_dict(j, orient='columns')

left lance
#

unfortunately same error

#

GRAH!

#
j_listed = {k: [v] for k, v in j.items()}
df = pd.DataFrame.from_dict(j_listed)
print(df)
#

that works

#

I just dont understands why pandas doesnt see not a list and then if all of them are 1 deep just put it in what else would anyone being doing with it

heady bolt
#

I mean sure. But a table of size 1 is not the typical use case.

versed walrus
#

BTW, curious to know why you're storing something which isn't a grid of values in a dataframe? As @molten marsh says, that's pretty unusual.

#

<strike>BTW your dict values arenot a list. You could convert them to lists:

j2 = { k: [v] for k, v in j.items() }

</strike>
I see you've just done this, sorry.

mortal shardBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.