0
import pandas as pd
from smartapi import SmartConnect
from smartapi import SmartWebSocket
import json
obj = SmartConnect(api_key="vhidyTNq")

refreshToken = data['data']['refreshToken']
feedToken = obj.getfeedToken()
ss = SmartWebSocket(feedToken,"P78013")
 
obj=SmartConnect(api_key="vhidyTNq")
data = obj.generateSession("P78013","Ujhdy@2")
print(data)

parsed = json.loads(data)

print(parsed.jwtToken)

The result printed in the following format

{'status': True, 'message': 'SUCCESS', 'errorcode': '', 'data': {'clientcode': 'K98913', 'name': 'HPP', 'email': '', 'mobileno': '', 'exchanges': ['bse_cm', 'cde_fo', 'mcx_fo', 'ncx_fo', 'nse_cm', 'nse_fo'], 'products': ['CNC', 'NRML', 'MARGIN', 'MIS', 'BO', 'CO'], 'lastlogintime': '', 'broker': '', 'jwtToken': 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VybmFtZSI6Iko4ODkxMyIsInJvbGVzIjowLCJ1c2VydHlwZSI6IlVTRVIiLCJpYXQiOjE2NTU3NTAxNDksImV4cCI6MTc0MjE1MDE0OX0.P1Ne0T0lTgScZJ1udMYRaJ32WeNDB-bZIwMg4uSAGC4RDFnYRsdvXGRyIEx7KS1LpQ6ndRIt7UjoyIewCs7HLA', 'refreshToken': 'eyJhbGciOiJIUzUxMiJ9.eyJ0b2tlbiI6IlJFRlJFU0gtVE9LRU4iLCJpYXQiOjE2NTU3NTAxNDl9.9DM1ggWfaervPe3qCpoDywfdb8kJ6okQrqZeR_mjsbGliqM7w0DdRyxTHyB7m-742Sfj9tVsZ4qQrOK0RQ9TmQ'}}

i am trying to filter out the 'jwtToken' value in the string format like below

jwtToken='Bearer eyJhbGciOiJIUzUxMiJ9.eyJ1c2VybmFtZSI.....'

0

2 Answers 2

1

Update:

It seems that data is a dict, then you can directly use data['data']['jwtToken'] to get the result.


If data is a string, you can try using regular expression:

import re
re.search(r"'jwtToken': '(.*?)'", data).group(1)
Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

print(parsed["data"]["jwtToken"])

Also, give the full code of your project will be appreciated.

2 Comments

I get the following error TypeError: the JSON object must be str, bytes or bytearray, not 'dict'
I updated the code now. Sorry for not testing your code. @jyotirmay

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.