Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Have a dict in double quoted string trying to convert it into normal dict using json.loads but getting json decode error

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)

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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'))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading