0

This is my code:

    req = urllib.request.Request(url, headers=hdr)
    req.get_method = lambda: 'GET'
    response = urllib.request.urlopen(req)
    ### Response: 200 OK
    print("If Response=200 Script run is OK : " , response.getcode())
    print(response.read())

and the result is:

If Response=200 Script run is OK: 200

b'[{"id":"d85b3704-60c9-XXXXXXX-2886629c732e","name":"XXXXXX-d85b37","location":"Trial","accountType":"Trial","url":"/www.ai/","accessToken":"eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SWQiOiJkODViMzcwNC02MGM5LTQ3NGYtYWVjYy0yODg2NjI5YzczMmUiLCJQZXJtaXNzaW9uIjoiQ29udHJpYnV0b3IiLCJFeHRlcm5hbFVzZXJJZCI6IjExNjAyMDI3NDYxNTkyMzc5NTU5MCIsIlVzZXJUeXBlIjoiR29vZ2xlIiwiSXNzdWVyTG9jYXRpb24iOiJUcmlhbCIsIm5iZiI6MTY2MzUzMzA4OSwiZXhwIjoxNjYzNTM2OTg5LCJpc3MiOiJodHRwczovL2FwaS52aWRlb2luZGV4ZXIuYWkvIiwiYXVkIjoiaHR0cHM6Ly9hcGkudmlkZW9pbmRleGVyLmFpLyJ9.VDoLlEdExAB0xMBP_pB6oN2DBL-9BQS0vEhpAq8d2o0","moveToArmStartedDate":"0001-01-01T00:00:00"}]'

How can I just filter and show only 'accessToken' in the output?

0

1 Answer 1

1

Pretty Simple:

resp: bytes = b'[{"id":"d85b3704-60c9-XXXXXXX-2886629c732e","name":"XXXXXX-d85b37","location":"Trial","accountType":"Trial","url":"/www.ai/","accessToken":"eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJBY2NvdW50SWQiOiJkODViMzcwNC02MGM5LTQ3NGYtYWVjYy0yODg2NjI5YzczMmUiLCJQZXJtaXNzaW9uIjoiQ29udHJpYnV0b3IiLCJFeHRlcm5hbFVzZXJJZCI6IjExNjAyMDI3NDYxNTkyMzc5NTU5MCIsIlVzZXJUeXBlIjoiR29vZ2xlIiwiSXNzdWVyTG9jYXRpb24iOiJUcmlhbCIsIm5iZiI6MTY2MzUzMzA4OSwiZXhwIjoxNjYzNTM2OTg5LCJpc3MiOiJodHRwczovL2FwaS52aWRlb2luZGV4ZXIuYWkvIiwiYXVkIjoiaHR0cHM6Ly9hcGkudmlkZW9pbmRleGVyLmFpLyJ9.VDoLlEdExAB0xMBP_pB6oN2DBL-9BQS0vEhpAq8d2o0","moveToArmStartedDate":"0001-01-01T00:00:00"}]'
import json


data = json.loads(resp)
print(data[0]['accessToken'])

Okay, how does this work now? We load the response first into a Dictionary. Then we simply take the first element of the list (because your response was a list, idk why) and take the acessToken with ['accessToken'].

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

2 Comments

Thanks, it is working properly. But is there a possibility to streamline the first line? to something like this: resp: bytes = json.read() instead b'[{"id":"d85b3704-60c9-XXXXXXX-2886629c732e","name":"XXXXXX-d85b37...
Replace resp: bytes = xxxxx with resp: bytes = response.read(). Or if you wan't it short, do data = json.loads(response.read()).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.