Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSONDecodeError #14

Open
DSBay opened this issue Nov 19, 2023 · 0 comments
Open

JSONDecodeError #14

DSBay opened this issue Nov 19, 2023 · 0 comments

Comments

@DSBay
Copy link

DSBay commented Nov 19, 2023

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant