Exemple #1
0
def find_properties_object(path: List[str], field: str, properties) -> Dict[str, Dict]:
    """
    This function is trying to look for a nested "properties" node under the current JSON node to
    identify all nested objects.

    @param path JSON path traversed so far to arrive to this node
    @param field is the current field being considered in the Json Tree
    @param properties is the child tree of properties of the current field being searched
    """
    result = {}
    current_path = path + [field]
    current = "_".join(current_path)
    if isinstance(properties, str) or isinstance(properties, int):
        return {}
    else:
        if "items" in properties:
            return find_properties_object(path, field, properties["items"])
        elif "properties" in properties:
            # we found a properties object
            return {current: properties["properties"]}
        elif "type" in properties and is_simple_property(properties["type"]):
            # we found a basic type
            return {current: {}}
        elif isinstance(properties, dict):
            for key in properties.keys():
                child = find_properties_object(path=current_path, field=key, properties=properties[key])
                if child:
                    result.update(child)
        elif isinstance(properties, list):
            for item in properties:
                child = find_properties_object(path=current_path, field=field, properties=item)
                if child:
                    result.update(child)
    return result
Exemple #2
0
 def extract_json_column(property_name: str, json_column_name: str, definition: Dict, column_name: str) -> str:
     json_path = [property_name]
     json_extract = jinja_call(f"json_extract({json_column_name}, {json_path})")
     if "type" in definition:
         if is_array(definition["type"]):
             json_extract = jinja_call(f"json_extract_array({json_column_name}, {json_path})")
         elif is_object(definition["type"]):
             json_extract = jinja_call(f"json_extract({json_column_name}, {json_path})")
         elif is_simple_property(definition["type"]):
             json_extract = jinja_call(f"json_extract_scalar({json_column_name}, {json_path})")
     return f"{json_extract} as {column_name}"