def jq_filter(value, filter_expression, all=False): """ Parse input with jq language. """ if not HAS_JQ: raise AnsibleError(missing_required_lib("jq")) if all: return jq.all(filter_expression, value) else: return jq.first(filter_expression, value)
def transform_with_jq(data: Any, jq_filter: str) -> Union[PdDataList, PdDataDict]: """Apply a jq filter on data before it's passed to a pd.DataFrame""" all_data: Union[List[PdDataList], List[PdDataDict], PdDataList] = jq.all(jq_filter, data) # If the data is already presented as a list of rows, # then undo the nesting caused by "multiple_output" jq option if len(all_data) == 1 and ( isinstance(all_data[0], list) # List[PdDataList] # detects another valid datastructure [{col1:[value, ...], col2:[value, ...]}] or (isinstance(all_data[0], dict) and isinstance( list(all_data[0].values())[0], list)) # List[PdDataDict] ): return all_data[0] else: return cast(PdDataList, all_data)
def all_function_with_json_text_input_returns_all_output_element_in_list( self): output = jq.all(".[] + 1", text="[1, 2, 3]") assert_equal([2, 3, 4], output)
def all_function_with_json_value_input_returns_all_output_elements_in_list( self): output = jq.all(".[] + 1", [1, 2, 3]) assert_equal([2, 3, 4], output)