Trying to get the dictionary from string using json.loads but getting Exception JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
.
Below is the exact string value in contract_details
for anyone to try:
contract_details = "{'instrumentToken': 17098, 'instrumentName': 'BANKNIFTY', 'name': nan, 'lastPrice': 333.05, 'expiry': '07OCT21', 'strike': 37800.0, 'tickSize': 0.05, 'lotSize': 25, 'instrumentType': 'OI', 'segment': 'FO', 'exchange': 'NSE', 'isin': nan, 'multiplier': 1, 'exchangeToken': 40728, 'optionType': 'PE'}"
contract_details = current_order["contract_details"]
contract_details = json.loads(contract_details)
CodePudding user response:
As the error message Expecting property name enclosed in double quotes
suggests , json
expects strings to have double quotes around the values inside the string. You can change the source if possible or replace the single quotes with double quotes using replace("'", '"')
on contract_details
, you also need to replace 'nan'
with 'NaN'
json.loads(contract_details.replace("'", '"').replace('nan', 'NaN'))