Untitled

 avatar
unknown
plain_text
5 months ago
2.1 kB
4
Indexable
#Convert into Big Query Schema
def convert_json_to_valid_schema(json):
    rec_to_insert = []

    no_of_recs= len(json_data['features'][0])
    print(f"{no_of_recs} to be processed!!!")

    def convert_coord_list_to_string(list):
        # Flatten the list of lists into a single list
        flattened_list = [str(item) for sublist in list_of_lists for item in sublist]

        # Join all the elements with a comma
        result_string = ','.join(flattened_list)

        return result_string


    for i in range(0,no_of_recs):

        

        schema_format = {
        "geometry": {'coordinates':convert_coord_list_to_string(json_data['features'][i]['geometry']['coordinates']),'type':json_data['features'][i]['geometry']['type']},
        "warningImpact": json_data['features'][i]['properties']['warningImpact'],
        "validFromDate": json_data['features'][i]['properties']['validFromDate'],
        "warningFurtherDetails": json_data['features'][i]['properties']['warningFurtherDetails'],
        "warningHeadline": json_data['features'][i]['properties']['warningHeadline'],
        "affectedAreas": json_data['features'][i]['properties']['affectedAreas'],
        "issuedDate": json_data['features'][i]['properties']['issuedDate'],
        "warningLevel": json_data['features'][i]['properties']['warningLevel'],
        "validToDate": json_data['features'][i]['properties']['validToDate'],
        "modifiedDate": json_data['features'][i]['properties']['modifiedDate'],
        "warningId": json_data['features'][i]['properties']['warningId'],
        "warningStatus": json_data['features'][i]['properties']['warningStatus'],
        "warningLikelihood": json_data['features'][i]['properties']['warningLikelihood'],
        "whatToExpect": json_data['features'][i]['properties']['whatToExpect'],
        "weatherType": json_data['features'][i]['properties']['weatherType'],
        "warningVersion": json_data['features'][i]['properties']['warningVersion'],
        }

        rec_to_insert.append(schema_format)

    return rec_to_insert
Editor is loading...
Leave a Comment