code: def json_to_key_value(json_data, key_field, value_field=None, array=False):
          
          if not isinstance(json_data, list):
              json_data = [json_data]
  
          key_value_array = []
          for d in json_data:
              if d.get(key_field) is not None and value_field is None:
                  # key / value based off same entry
                  key = key_field
                  value = d.get(key_field)
              elif d.get(key_field) is not None and d.get(value_field) is not None:
                  # key / value data are from separate entries
                  key = d.get(key_field)
                  value = d.get(value_field)
              else:
                  continue
  
              key_value_array.append({'key': key, 'value': value})
  
          if len(key_value_array) == 1 and not array:
              return key_value_array[0]
  
          return key_value_array
docs: Convert JSON data to a KeyValue/KeyValueArray.
  
          Args:
              json_data (dictionary|list): Array/List of JSON data.
              key_field (string): Field name for the key.
              value_field (string): Field name for the value or use the value of the key field.
              array (boolean): Always return array even if only on result.
  
          Returns:
              (dictionary|list): A dictionary or list representing a KeyValue or KeyValueArray.