<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def __parse_identities(self, json):
 """Parse identities using Stackalytics format. The Stackalytics identities format is a JSON document under the "users" key. The document should follow the next schema: { "users": [ { "launchpad_id": "0-jsmith", "gerrit_id": "jsmith", "companies": [ { "company_name": "Example", "end_date": null } ], "user_name": "John Smith", "emails": ["jsmith@example.com", "jsmith@example.net"] }, { "companies": [ { "company_name": "Bitergia", "end_date": null }, { "company_name": "Example", "end_date": "2010-Jan-01" } ], "user_name": "John Doe", "emails": ["jdoe@bitergia.com", "jdoe@example.com"] } ] } :parse json: JSON object to parse :raise InvalidFormatError: raised when the format of the JSON is not valid. """

        try:
            for user in json['users']:
                name = self.__encode(user['user_name'])
                uuid = name

                uid = UniqueIdentity(uuid=uuid)
                identity = Identity(name=name, email=None, username=None,
                                    source=self.source, uuid=uuid)
                uid.identities.append(identity)

                for email_addr in user['emails']:
                    email = self.__encode(email_addr)

                    identity = Identity(name=name, email=email, username=None,
                                        source=self.source, uuid=uuid)
                    uid.identities.append(identity)

                for site_id in ['gerrit_id', 'launchpad_id']:
                    username = user.get(site_id, None)

                    if not username:
                        continue

                    username = self.__encode(username)
                    source = self.source + ':' + site_id.replace('_id', '')
                    identity = Identity(name=name, email=None, username=username,
                                        source=source, uuid=uuid)
                    uid.identities.append(identity)

                for rol in self.__parse_enrollments(user):
                    uid.enrollments.append(rol)

                self._identities[uuid] = uid
        except KeyError as e:
            msg = "invalid json format. Attribute %s not found" % e.args
            raise InvalidFormatError(cause=msg)