selva221724 / pypostalwin

libpostal wrapper python package for windows
MIT License
11 stars 5 forks source link

JSONDecodeError #14

Open DSBay opened 8 months ago

DSBay commented 8 months ago

parse_address function returns **JSONDecodeError:** Extra data:

To fix this, I've added some code to split the json objects that are returned from libpostal's address_parser.exe and then iteratively run json.loads() on the objects and append them to a list.

#old code:
    def parse_address(self, address):
        self._validate_address(address)
        address = self._remove_special_chars(address)
        address = address + '\n'
        command = [self.address_parser_path]
        result = self._run_command(command, input_data=address)
        json_start = result.find('{')
        json_end = result.rfind('}') + 1
        json_data = result[json_start:json_end]
        return json.loads(json_data)
#new code:
    def parse_address(self, address):
        self._validate_address(address)
        address = self._remove_special_chars(address)
        address += '\n'
        command = [self.address_parser_path]
        result = self._run_command(command, input_data=address)
        #print('result:', result, sep='\n', end='\n')

        json_start = result.find('{')
        json_end = result.rfind('}') + 1
        json_data = result[json_start:json_end]
        json_objs = json_data.split('\n\nResult:\n')
        parsed_res = []

        for json_obj in json_objs:
            obj_start = json_obj.find('{')
            obj_end = json_obj.rfind('}') + 1
            if obj_start != -1 and obj_end != -1:
                json_data = json_obj[obj_start:obj_end]
                #print('json:', json_data, sep='\n', end='\n')

                try:
                    parsed_json = json.loads(json_data)
                    parsed_res.append(parsed_json)

                except json.JSONDecodeError as e:
                    print(f'Error parsing JSON: {e}')

        return parsed_res