<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _collect_settings(self, apps):
 """ Iterate over given apps or INSTALLED_APPS and collect the content of each's settings file, which is expected to be in JSON format. """

        contents = {}
        if apps:
            for app in apps:
                if app not in settings.INSTALLED_APPS:
                    raise CommandError("Application '{0}' not in settings.INSTALLED_APPS".format(app))
        else:
            apps = settings.INSTALLED_APPS
        for app in apps:
            module = import_module(app)
            for module_dir in module.__path__:
                json_file = os.path.abspath(os.path.join(module_dir, self.json_file))
                if os.path.isfile(json_file):
                    with open(json_file, 'r') as fp:
                        contents[app] = json.load(fp)
        return contents