refactor check order in _flatten_key_value_pair