Search code examples
pythonjsonhttprequestresponse

http request response not in json format


I've spent a lot of time trying to fix this but still no luck. appreciate if you could help.

This is in Python

When I run this:

import requests
import json

response = requests.post(url)
response.text

I get this:

'\n\n\n\n{\nVisitCount : "9,992",\ntotalCount : "18,018",\nnotiList : [\n\n],\nPassed : false\n}'

If I run this:

print(response.text)

I get this:

{ VisitCount : "9,992", totalCount : "18,018,455", notiList : [

], Passed : false }

Ultimately I am aiming for extracting the number 9,992. I thought easiest way is to convert this into json, but it doesn't seem to work.

When I run:

response.json() or json.loads(response.text)

I get this:

JSONDecodeError: Expecting property name enclosed in double quotes: line 6 column 1 (char 6)

Which is probably because it's missing the double quotes in the response.

  1. how can I fix this problem, probably adding double quotes to the response?
  2. is there an easier way of extracting the number, which in this case is 9,992?

Solution

  • You can use regexes:

    import re
    number_string = re.findall("VisitCount\W+:\W+.*?([0-9,]+).*", response.text.strip())[0]
    

    gives number_string as

    9,992
    

    If you want to convert it into int:

    number = int(number_string.replace(",", ""))