需要帮助将 Ruby 函数转换为 Python 函数

Need help on converting Ruby function to Python function

我正在尝试创建一个 Python 函数来将列表(ELK 术语中的数组对象)转换为字典。我找到了一个示例 Ruby 函数来执行此操作,我正在尝试将其转换为 Python 函数以供我使用。我发现很难获得输出。输出将被插入回 Elastic Search。

输入

{
  "foo": "bar",
  "test": {
    "steps": [
      {
        "response_time": "100"
      },
      {
        "response_time": "101",
        "more_nested": [
          {
            "hello": "world"
          },
          {
            "hello2": "world2"
          }
        ]
      }
    ]
  }
}

**

预期输出

**

{
  "foo": "bar",
  "test": {
    "steps": {
      "0": {
        "response_time": "100"
      },
      "1": {
        "response_time": "101",
        "more_nested": {
          "0": {
            "hello": "world"
          },
          "1": {
            "hello2": "world2"
          }
        }
      }
    }
  }
}

当前O/P

{'0': {'response_time': '100'},
 '1': {'more_nested': [{'hello': 'world'}, {'hello2': 'world2'}],
  'response_time': '101'}}

原始脚本停止了对列表的检查,没有实现字典列表的解决方案。现在看起来不错

def array_path(my_dict):
    if type(my_dict) is dict:
        for k, v in my_dict.items():
            my_dict[k] = array_path(v)
    elif type(my_dict) is list:
        return {str(i): array_path(item) for i, item in enumerate(my_dict)}
    return my_dict