<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def build_datasets(datasets_json):
 """ Given datasets in JSON format, build and return a list of dataset objects """

    if not datasets_json:
        return []

    datasets = []
    dataset_type_map = OrderedDict([
        ('generated', 'datasets'),
        ('used', 'prev_published_datasets')
    ])
    dataset_type_map_found = []
    # First look for the types of datasets present
    for dataset_key, dataset_type in iteritems(dataset_type_map):
        if datasets_json.get(dataset_key):
            dataset_type_map_found.append(dataset_key)
    # Continue with the found dataset types
    for dataset_key in dataset_type_map_found:
        dataset_type = dataset_type_map.get(dataset_key)
        for dataset_values in datasets_json.get(dataset_key):
            dataset = ea.Dataset()
            utils.set_attr_if_value(dataset, 'dataset_type', dataset_type)
            utils.set_attr_if_value(dataset, 'year', dataset_values.get('date'))
            utils.set_attr_if_value(dataset, 'title', dataset_values.get('title'))
            utils.set_attr_if_value(dataset, 'comment', dataset_values.get('details'))
            utils.set_attr_if_value(dataset, 'doi', dataset_values.get('doi'))
            utils.set_attr_if_value(dataset, 'uri', dataset_values.get('uri'))
            utils.set_attr_if_value(dataset, 'accession_id', dataset_values.get('dataId'))
            utils.set_attr_if_value(dataset, 'assigning_authority', 
                                    dataset_values.get('assigningAuthority'))
            # authors
            if dataset_values.get('authors'):
                # parse JSON format authors into author objects
                for author_json in dataset_values.get('authors'):
                    if utils.author_name_from_json(author_json):
                        dataset.add_author(utils.author_name_from_json(author_json))
            # Try to populate the doi attribute if the uri is a doi
            if not dataset.doi and dataset.uri:
                if dataset.uri != eautils.doi_uri_to_doi(dataset.uri):
                    dataset.doi = eautils.doi_uri_to_doi(dataset.uri)
            datasets.append(dataset)
    return datasets