Home > Software design >  Improperly formatted JSON, no idea what is incorrect
Improperly formatted JSON, no idea what is incorrect

Time:02-03

I have a JSON string here. In Python, whenever I json.loads(string), I get json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 21 (char 20). However, I am completely lost on why it is an improperly formatted JSON. I have tried adding r infront of the string but with no avail.

JSON String:

string = '{"alarm_flood": "[{"Date_Time": "01-12-2009 12:18:42", "flood_status": "1", "FloodIndex": "0", "Plant_Area": "AREA 1"}, {"Date_Time": "01-12-2009 12:18:42", "flood_status": "0", "FloodIndex": "0", "Plant_Area": "AREA 1"}]", "flood_status": "[{"Date_Time": "01-12-2009 12:18:42", "flood_status": "1", "FloodIndex": "0", "Plant_Area": "AREA 1"}, {"Date_Time": "01-12-2009 12:18:42", "flood_status": "0", "FloodIndex": "0", "Plant_Area": "AREA 1"}]"}'

Can someone point me to the right direction?

EDIT: The quotes were indeed the issue. I had to put them in there because another end point that I'm hitting requires those. But I will ask the owner of that endpoint to change their structure.

CodePudding user response:

You had some extra quotation marks. Here is the corrected version:

string = '{"alarm_flood": [{"Date_Time": "01-12-2009 12:18:42", "flood_status": "1", "FloodIndex": "0", "Plant_Area": "AREA 1"}, {"Date_Time": "01-12-2009 12:18:42", "flood_status": "0", "FloodIndex": "0", "Plant_Area": "AREA 1"}], "flood_status": [{"Date_Time": "01-12-2009 12:18:42", "flood_status": "1", "FloodIndex": "0", "Plant_Area": "AREA 1"}, {"Date_Time": "01-12-2009 12:18:42", "flood_status": "0", "FloodIndex": "0", "Plant_Area": "AREA 1"}]}'

CodePudding user response:

you have some extra double quotes, you can remove them using replace

string = string.replace('"[', '[').replace(']"', ']');
  • Related