piwebasync/api/util.py
def search_response_content(field: str, response: JSONType) -> List[JSONType]:
    """
    Extract field values from nested JSON response. Fields should be
    specified according to hierarchy of the data using dot notation,
    ``Top.Nested.NestedNested``.
    This function groups fields in nested lists by the index in the list
    and returns a list of lists. For example consider the json response below...
    Items: [
        {
            Obj: 1,
            Items: [
                {Value: 1},
                {Value: 2},
            ]
        },
        {
           Obj: 2,
            Items: [
                {Value: 3},
                {Value: 4},
            ]
        }
    ]
    A field value of "Items.Items.Value" will produce the following output...
    [[1, 2], [3, 4]]
    """

    def extract_nested_fields_from_list(
        nested_fields: List[str],
        subset: List[JSONType],
        level: int = 0
    ) -> List[JSONType]:
        items = []
        for item in subset:
            if isinstance(item, dict):
                items.append(extract_nested_fields_from_dict(nested_fields, item, level))
            elif isinstance(item, list):
                items.append(extract_nested_fields_from_list(nested_fields, item, level))
            else:
                items.append(item)
        return items

    def extract_nested_fields_from_dict(
        nested_fields: List[str],
        subset: dict,
        level: int = 0
    ) -> Union[JSONType, List[JSONType]]:
        nested_field = nested_fields[level]
        try:
            item = subset[nested_field]
        except KeyError:
            level -= 1
            return
        if isinstance(item, list):
            level += 1
            return extract_nested_fields_from_list(nested_fields, item, level)
        elif isinstance(item, dict):
            level += 1
            return extract_nested_fields_from_dict(nested_fields, item, level)
        else:
            level -= 1
            return item
            
    nested_fields = field.split(".")
    nested_field = nested_fields.pop(0)
    try:
        subset = response[nested_field]
        if isinstance(subset, dict) and nested_fields:
            return extract_nested_fields_from_dict(nested_fields, subset)
        elif isinstance(subset, list) and nested_fields:
            return extract_nested_fields_from_list(nested_fields, subset)
        else:
            return [subset]
    except KeyError:
        return []

    Extract field values from nested JSON response. Fields should be
    specified according to hierarchy of the data using dot notation,
    ``Top.Nested.NestedNested``.
    This function groups fields in nested lists by the index in the list
    and returns a list of lists. For example consider the json response below...
    Items: [
        {
            Obj: 1,
            Items: [
                {Value: 1},
                {Value: 2},
            ]
        },
        {
           Obj: 2,
            Items: [
                {Value: 3},
                {Value: 4},
            ]
        }
    ]
    A field value of "Items.Items.Value" will produce the following output...
    [[1, 2], [3, 4]]
    
Extract field values from nested JSON response. Fields should be
specified according to hierarchy of the data using dot notation,
``Top.Nested.NestedNested``.
This function groups fields in nested lists by the index in the list
and returns a list of lists.
Extract field values from nested JSON response . Fields should be specified according to hierarchy of the data using dot notation ` ` Top . Nested . NestedNested ` ` . This function groups fields in nested lists by the index in the list and returns a list of lists .
def search_response_content(field: str, response: JSONType) -> List[JSONType]:
    def extract_nested_fields_from_list(
        nested_fields: List[str],
        subset: List[JSONType],
        level: int = 0
    ) -> List[JSONType]:
        items = []
        for item in subset:
            if isinstance(item, dict):
                items.append(extract_nested_fields_from_dict(nested_fields, item, level))
            elif isinstance(item, list):
                items.append(extract_nested_fields_from_list(nested_fields, item, level))
            else:
                items.append(item)
        return items
    def extract_nested_fields_from_dict(
        nested_fields: List[str],
        subset: dict,
        level: int = 0
    ) -> Union[JSONType, List[JSONType]]:
        nested_field = nested_fields[level]
        try:
            item = subset[nested_field]
        except KeyError:
            level -= 1
            return
        if isinstance(item, list):
            level += 1
            return extract_nested_fields_from_list(nested_fields, item, level)
        elif isinstance(item, dict):
            level += 1
            return extract_nested_fields_from_dict(nested_fields, item, level)
        else:
            level -= 1
            return item
    nested_fields = field.split(".")
    nested_field = nested_fields.pop(0)
    try:
        subset = response[nested_field]
        if isinstance(subset, dict) and nested_fields:
            return extract_nested_fields_from_dict(nested_fields, subset)
        elif isinstance(subset, list) and nested_fields:
            return extract_nested_fields_from_list(nested_fields, subset)
        else:
            return [subset]
    except KeyError:
        return []
def search_response_content ( field : str , response : JSONType ) -> List [ JSONType ] : def extract_nested_fields_from_list ( nested_fields : List [ str ] , subset : List [ JSONType ] , level : int = 0 ) -> List [ JSONType ] : items = [ ] for item in subset : if isinstance ( item , dict ) : items . append ( extract_nested_fields_from_dict ( nested_fields , item , level ) ) elif isinstance ( item , list ) : items . append ( extract_nested_fields_from_list ( nested_fields , item , level ) ) else : items . append ( item ) return items def extract_nested_fields_from_dict ( nested_fields : List [ str ] , subset : dict , level : int = 0 ) -> Union [ JSONType , List [ JSONType ] ] : nested_field = nested_fields [ level ] try : item = subset [ nested_field ] except KeyError : level -= 1 return if isinstance ( item , list ) : level += 1 return extract_nested_fields_from_list ( nested_fields , item , level ) elif isinstance ( item , dict ) : level += 1 return extract_nested_fields_from_dict ( nested_fields , item , level ) else : level -= 1 return item nested_fields = field . split ( "." ) nested_field = nested_fields . pop ( 0 ) try : subset = response [ nested_field ] if isinstance ( subset , dict ) and nested_fields : return extract_nested_fields_from_dict ( nested_fields , subset ) elif isinstance ( subset , list ) and nested_fields : return extract_nested_fields_from_list ( nested_fields , subset ) else : return [ subset ] except KeyError : return [ ]
Extract field values from nested JSON response.
Extract field values from nested JSON response .
"""
    Extract field values from nested JSON response. Fields should be
    specified according to hierarchy of the data using dot notation,
    ``Top.Nested.NestedNested``.
    This function groups fields in nested lists by the index in the list
    and returns a list of lists. For example consider the json response below...
    Items: [
        {
            Obj: 1,
            Items: [
                {Value: 1},
                {Value: 2},
            ]
        },
        {
           Obj: 2,
            Items: [
                {Value: 3},
                {Value: 4},
            ]
        }
    ]
    A field value of "Items.Items.Value" will produce the following output...
    [[1, 2], [3, 4]]
    """
field
str
response
JSONType