1

i am getting a json data after hitting an API . when i try to load that json into python using json.loads(response.text), I am getting a delimiter error .

when checked few fields in json dose not have "," separating them.

{
"id":"142379",
"label":"1182_Mailer_PROD",
"location":"Bangalore, India",
"targetType":"HTTPS performance",
"frequency":"15",
"fails":"2764",
"totalUptime":"85.32"
"tests":[
{"date":"09-24-2019 09:31","status":"Could not resolve: mailer.accenture.com (DNS server returned answer with no data)","responseTime":"0.000","dnsTime":"0.000","connectTime":"0.000","redirectTime":"0.000","firstbyteTime":"0.000","lastbyteTime":"0.000","pingLoss":"0.00","pingMin":"0.000","pingAvg":"0.000","pingMax":"0.000","size":"0","md5hash":"(null)"}
]
}
,
{
"id":"158651",
"label":"11883-GR dd-WSP",
"location":"Chicago, IL",
"targetType":"Performance transaction",
"frequency":"15",
"fails":"5919",
"totalUptime":"35.14"
,"tests":[
{"date":"09-24-2019 09:26","status":"Keywords not found - Working","responseTime":"0.669","stepresults":[
{"stepid":"1","date":"09-24-2019 09:26","status":"OK","responseTime":"0.453","dnsTime":"0.000","connectTime":"0.025","redirectTime":"0.264","firstbyteTime":"0.141","lastbyteTime":"0.024","size":"22351","md5hash":"ca002cf662980511a9faa88286f2ee96"},
{"stepid":"2","date":"09-24-2019 09:26","status":"Keywords not found - Working","responseTime":"0.216","dnsTime":"0.000","connectTime":"0.023","redirectTime":"0.000","firstbyteTime":"0.171","lastbyteTime":"0.022","size":"22457","md5hash":"38327404e4f2392979aa7dfa27118f4e"}
]}]
}

This is a small chunk of data from the response , as you could see "totalUptime":"85.32" doesn't have a comma separating it.

could you please let me know how can we load the data into python object even though the json is deformed

Sumanth Shetty
  • 587
  • 1
  • 8
  • 19
  • it may help if you can provide a sample of response.text – henrywongkk Sep 25 '19 at 05:53
  • @henrywongkk modified the question – Sumanth Shetty Sep 25 '19 at 06:00
  • Maybe you could use regex to fix it. Behind every 4th quote that is not followed by a closing bracket, must be a comma. Something along those lines. Still, I hope someone can tell us that there is a more elegant way. Maybe this helps, https://stackoverflow.com/questions/375104/how-can-i-match-a-quote-delimited-string-with-a-regex – The Fool Sep 25 '19 at 06:07
  • maybe raise an issue with the API maintainer/provider? Also, if using `requests` and if/when getting supposedly JSON response, you can do just `response.json()` – buran Sep 25 '19 at 07:31

1 Answers1

0

Deformed JSON is not JSON, so obviously you can't load it with a standard procedure. There are only two possibilities to load it:

  • Create your own parser
  • Modify the input to conform to the JSON standard

Both possibilities need you to define what format do you want to import. If it is OK for your format not to have commas then you have to define what your delimiters are.

From the example you posted is difficult to make any definitive assessment about how the input format is defined. So you probably will have to write a rudimentary parser and approximate it by try and error to the input you are trying to parse.

julodnik
  • 359
  • 2
  • 8