Error Regarding JSONDecoderError


  • Hello Team SmartAPI! For the past few days, I was trying to connect to the smartapi through my algo setup and it is giving me error. Here's the code for reference and Output. Can you please help me with this. I'm working on this after 2 months and I think API might got some changes, updates.

    CODE:

    Download master contract file and create token_df

    url = 'https://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json'
    d = requests.get(url).json()
    token_df = pd.DataFrame.from_dict(d)
    token_df['expiry'] = pd.to_datetime(token_df['expiry']).apply(lambda x: x.date())
    token_df = token_df.astype({'strike': float})

    def getTokenInfo(symbol, exch_seg='NSE', instrumenttype='OPTIDX', strike_price='', pe_ce='CE', expiry_day=None):
    df = token_df
    strike_price = strike_price * 100
    if exch_seg == 'NSE':
    eq_df = df[df['exch_seg'] == 'NSE']
    return eq_df[eq_df['name'] == symbol]
    elif exch_seg == 'NFO' and (instrumenttype in ['FUTSTK', 'FUTIDX']):
    return df[(df['exch_seg'] == 'NFO') & (df['instrumenttype'] == instrumenttype) & (df['name'] == symbol)].sort_values(by=['expiry'])
    elif exch_seg == 'NFO' and (instrumenttype in ['OPTSTK', 'OPTIDX']):
    filtered_df = df[(df['exch_seg'] == 'NFO') &
    (df['expiry'] == expiry_day) &
    (df['instrumenttype'] == instrumenttype) &
    (df['name'] == symbol) &
    (df['strike'] == strike_price) &
    (df['symbol'].str.endswith(pe_ce))].sort_values(by=['expiry'])
    #print(f"Filtered DF for {symbol} {expiry_day} {strike_price} {pe_ce}:\n", filtered_df)
    return filtered_df

    OUTPUT :

    [I 250303 14:06:26 smartConnect:121] in pool
    [I 250303 14:06:31 3702498481:21] Session created successfully.

    JSONDecodeError Traceback (most recent call last)
    File ~\AppData\Roaming\Python\Python310\site-packages\requests\models.py:971, in Response.json(self, **kwargs)
    970 try:
    --> 971 return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973 # Catch JSON-related errors and raise as requests.JSONDecodeError
    974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError

    File D:\Anaconda\lib\json_init_.py:346, in loads(s, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    343 if (cls is None and object_hook is None and
    344 parse_int is None and parse_float is None and
    345 parse_constant is None and object_pairs_hook is None and not kw):
    --> 346 return _default_decoder.decode(s)
    347 if cls is None:

    File D:\Anaconda\lib\json\decoder.py:337, in JSONDecoder.decode(self, s, _w)
    333 """Return the Python representation of s (a str instance
    334 containing a JSON document).
    335
    336 """
    --> 337 obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    338 end = _w(s, end).end()

    File D:\Anaconda\lib\json\decoder.py:353, in JSONDecoder.raw_decode(self, s, idx)
    352 try:
    --> 353 obj, end = self.scan_once(s, idx)
    354 except StopIteration as err:

    JSONDecodeError: Unterminated string starting at: line 1 column 409344 (char 409343)

    During handling of the above exception, another exception occurred:

    JSONDecodeError Traceback (most recent call last)
    Cell In[25], line 27
    25 # Download master contract file and create token_df
    26 url = 'https://margincalculator.angelbroking.com/OpenAPI_File/files/OpenAPIScripMaster.json'
    ---> 27 d = requests.get(url).json()
    28 token_df = pd.DataFrame.from_dict(d)
    29 token_df['expiry'] = pd.to_datetime(token_df['expiry']).apply(lambda x: x.date())

    File ~\AppData\Roaming\Python\Python310\site-packages\requests\models.py:975, in Response.json(self, **kwargs)
    971 return complexjson.loads(self.text, **kwargs)
    972 except JSONDecodeError as e:
    973 # Catch JSON-related errors and raise as requests.JSONDecodeError
    974 # This aliases json.JSONDecodeError and simplejson.JSONDecodeError
    --> 975 raise RequestsJSONDecodeError(e.msg, e.doc, e.pos)

    JSONDecodeError: Unterminated string starting at: line 1 column 409344 (char 409343)