I\'ve been trying to figure out a good way to load JSON objects in Python. I send this json data:
{\'http://example.org/about\': {\'http://purl.org/dc/terms/
x = x.replace("'", '"')
j = json.loads(x)
Although this is the correct solution, but it may lead to quite a headache if there a JSON like this -
{'status': 'success', 'data': {'equity': {'enabled': True, 'net': 66706.14510000008, 'available': {'adhoc_margin': 0, 'cash': 1277252.56, 'opening_balance': 1277252.56, 'live_balance': 66706.14510000008, 'collateral': 249823.93, 'intraday_payin': 15000}, 'utilised': {'debits': 1475370.3449, 'exposure': 607729.3129, 'm2m_realised': 0, 'm2m_unrealised': -9033, 'option_premium': 0, 'payout': 0, 'span': 858608.032, 'holding_sales': 0, 'turnover': 0, 'liquid_collateral': 0, 'stock_collateral': 249823.93}}, 'commodity': {'enabled': True, 'net': 0, 'available': {'adhoc_margin': 0, 'cash': 0, 'opening_balance': 0, 'live_balance': 0, 'collateral': 0, 'intraday_payin': 0}, 'utilised': {'debits': 0, 'exposure': 0, 'm2m_realised': 0, 'm2m_unrealised': 0, 'option_premium': 0, 'payout': 0, 'span': 0, 'holding_sales': 0, 'turnover': 0, 'liquid_collateral': 0, 'stock_collateral': 0}}}}
Noticed that "True" value? Use this to make things are double checked for Booleans. This will cover those cases -
x = x.replace("'", '"').replace("True", '"True"').replace("False", '"False"').replace("null", '"null"')
j = json.loads(x)
Also, make sure you do not make
x = json.loads(x)
It has to be another variable.
I have run into this problem multiple times when the JSON has been edited by hand. If someone was to delete something from the file without noticing it can throw the same error.
For instance, If your JSON last "}" is missing it will throw the same error.
So If you edit you file by hand make sure you format it like it is expected by the JSON decoder, otherwise you will run into the same problem.
Hope this helps!
Quite simply, that string is not valid JSON. As the error says, JSON documents need to use double quotes.
You need to fix the source of the data.
It is always ideal to use the json.dumps()
method.
To get rid of this error, I used the following code
json.dumps(YOUR_DICT_STRING).replace("'", '"')