Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/EmptyTag/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将这个硬json解析为数据帧?_Python_Json_Pandas_Dataframe - Fatal编程技术网

如何在python中将这个硬json解析为数据帧?

如何在python中将这个硬json解析为数据帧?,python,json,pandas,dataframe,Python,Json,Pandas,Dataframe,关于这个话题的另一个问题 在这个社区中,有几个问题和答案: 接下来是 但我认为在这种情况下,它不适用于这种硬json: 我的JSON: { "took":32, "timed_out":false, "_shards":{ "total":4, "successful":4, "skipped":0, "failed":0 }, "hits":{ "total":{ "va

关于这个话题的另一个问题

在这个社区中,有几个问题和答案:

  • 接下来是
  • 但我认为在这种情况下,它不适用于这种硬json:

    我的JSON:

        {
       "took":32,
       "timed_out":false,
       "_shards":{
          "total":4,
          "successful":4,
          "skipped":0,
          "failed":0
       },
       "hits":{
          "total":{
             "value":94,
             "relation":"eq"
          },
          "max_score":"None",
          "hits":[
    
          ]
       },
       "aggregations":{
          "hostname":{
             "doc_count_error_upper_bound":0,
             "sum_other_doc_count":0,
             "buckets":[
                {
                   "key":"396",
                   "doc_count":47,
                   "process_name":{
                      "doc_count_error_upper_bound":0,
                      "sum_other_doc_count":16,
                      "buckets":[
                         {
                            "key":"accounts-daemon",
                            "doc_count":2,
                            "process_state":{
                               "doc_count_error_upper_bound":0,
                               "sum_other_doc_count":0,
                               "buckets":[
                                  {
                                     "key":"sleeping",
                                     "doc_count":2,
                                     "process_pid":{
                                        "doc_count_error_upper_bound":0,
                                        "sum_other_doc_count":0,
                                        "buckets":[
                                           {
                                              "key":1092,
                                              "doc_count":2,
                                              "process_ppid":{
                                                 "doc_count_error_upper_bound":0,
                                                 "sum_other_doc_count":0,
                                                 "buckets":[
                                                    {
                                                       "key":1,
                                                       "doc_count":2,
                                                       "process_pgid":{
                                                          "doc_count_error_upper_bound":0,
                                                          "sum_other_doc_count":0,
                                                          "buckets":[
                                                             {
                                                                "key":1092,
                                                                "doc_count":2,
                                                                "process_cmdline":{
                                                                   "doc_count_error_upper_bound":0,
                                                                   "sum_other_doc_count":0,
                                                                   "buckets":[
                                                                      {
                                                                         "key":"/usr/lib/accountsservice/accounts-daemon",
                                                                         "doc_count":2,
                                                                         "process_username":{
                                                                            "doc_count_error_upper_bound":0,
                                                                            "sum_other_doc_count":0,
                                                                            "buckets":[
                                                                               {
                                                                                  "key":"root",
                                                                                  "doc_count":2,
                                                                                  "process_cwd":{
                                                                                     "doc_count_error_upper_bound":0,
                                                                                     "sum_other_doc_count":0,
                                                                                     "buckets":[
                                                                                        {
                                                                                           "key":"/",
                                                                                           "doc_count":2,
                                                                                           "process_cpu_start_time":{
                                                                                              "doc_count_error_upper_bound":0,
                                                                                              "sum_other_doc_count":0,
                                                                                              "buckets":[
                                                                                                 {
                                                                                                    "key":1570456346000,
                                                                                                    "key_as_string":"2019-10-07T13:52:26.000Z",
                                                                                                    "doc_count":2,
                                                                                                    "process_fd_limit_hard":{
                                                                                                       "value":4096.0
                                                                                                    },
                                                                                                    "process_fd_open":{
                                                                                                       "value":8.0
                                                                                                    },
                                                                                                    "process_memory_size":{
                                                                                                       "value":281055232.0
                                                                                                    },
                                                                                                    "process_memory_rss_bytes":{
                                                                                                       "value":6168576.0
                                                                                                    },
                                                                                                    "process_memory_share":{
                                                                                                       "value":5464064.0
                                                                                                    },
                                                                                                    "process_cpu_total_pct":{
                                                                                                       "value":0.0005
                                                                                                    },
                                                                                                    "process_fd_limit_soft":{
                                                                                                       "value":1024.0
                                                                                                    },
                                                                                                    "process_cpu_system_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_user_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_norm_pct":{
                                                                                                       "value":0.0005
                                                                                                    },
                                                                                                    "process_cpu_total_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_value":{
                                                                                                       "value":18775.0
                                                                                                    }
                                                                                                 }
                                                                                              ]
                                                                                           }
                                                                                        }
                                                                                     ]
                                                                                  }
                                                                               }
                                                                            ]
                                                                         }
                                                                      }
                                                                   ]
                                                                }
                                                             }
                                                          ]
                                                       }
                                                    }
                                                 ]
                                              }
                                           }
                                        ]
                                     }
                                  }
                               ]
                            }
                         }
                      ]
                   }
                },
                {
                   "key":"961",
                   "doc_count":47,
                   "process_name":{
                      "doc_count_error_upper_bound":0,
                      "sum_other_doc_count":16,
                      "buckets":[
                         {
                            "key":"accounts-daemon",
                            "doc_count":2,
                            "process_state":{
                               "doc_count_error_upper_bound":0,
                               "sum_other_doc_count":0,
                               "buckets":[
                                  {
                                     "key":"sleeping",
                                     "doc_count":2,
                                     "process_pid":{
                                        "doc_count_error_upper_bound":0,
                                        "sum_other_doc_count":0,
                                        "buckets":[
                                           {
                                              "key":1070,
                                              "doc_count":2,
                                              "process_ppid":{
                                                 "doc_count_error_upper_bound":0,
                                                 "sum_other_doc_count":0,
                                                 "buckets":[
                                                    {
                                                       "key":1,
                                                       "doc_count":2,
                                                       "process_pgid":{
                                                          "doc_count_error_upper_bound":0,
                                                          "sum_other_doc_count":0,
                                                          "buckets":[
                                                             {
                                                                "key":1070,
                                                                "doc_count":2,
                                                                "process_cmdline":{
                                                                   "doc_count_error_upper_bound":0,
                                                                   "sum_other_doc_count":0,
                                                                   "buckets":[
                                                                      {
                                                                         "key":"/usr/lib/accountsservice/accounts-daemon",
                                                                         "doc_count":2,
                                                                         "process_username":{
                                                                            "doc_count_error_upper_bound":0,
                                                                            "sum_other_doc_count":0,
                                                                            "buckets":[
                                                                               {
                                                                                  "key":"root",
                                                                                  "doc_count":2,
                                                                                  "process_cwd":{
                                                                                     "doc_count_error_upper_bound":0,
                                                                                     "sum_other_doc_count":0,
                                                                                     "buckets":[
                                                                                        {
                                                                                           "key":"/",
                                                                                           "doc_count":2,
                                                                                           "process_cpu_start_time":{
                                                                                              "doc_count_error_upper_bound":0,
                                                                                              "sum_other_doc_count":0,
                                                                                              "buckets":[
                                                                                                 {
                                                                                                    "key":1570450883000,
                                                                                                    "key_as_string":"2019-10-07T12:21:23.000Z",
                                                                                                    "doc_count":2,
                                                                                                    "process_fd_limit_hard":{
                                                                                                       "value":4096.0
                                                                                                    },
                                                                                                    "process_fd_open":{
                                                                                                       "value":8.0
                                                                                                    },
                                                                                                    "process_memory_size":{
                                                                                                       "value":281153536.0
                                                                                                    },
                                                                                                    "process_memory_rss_bytes":{
                                                                                                       "value":5992448.0
                                                                                                    },
                                                                                                    "process_memory_share":{
                                                                                                       "value":5279744.0
                                                                                                    },
                                                                                                    "process_cpu_total_pct":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_fd_limit_soft":{
                                                                                                       "value":1024.0
                                                                                                    },
                                                                                                    "process_cpu_system_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_user_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_norm_pct":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_value":{
                                                                                                       "value":13410.0
                                                                                                    }
                                                                                                 }
                                                                                              ]
                                                                                           }
                                                                                        }
                                                                                     ]
                                                                                  }
                                                                               }
                                                                            ]
                                                                         }
                                                                      }
                                                                   ]
                                                                }
                                                             }
                                                          ]
                                                       }
                                                    }
                                                 ]
                                              }
                                           }
                                        ]
                                     }
                                  }
                               ]
                            }
                         }
                      ]
                   }
                }
             ]
          }
       }
    }
    
       hostname     process_name process_state  ...  process_username  process_cwd    process_cpu_start_time
    0       396  accounts-daemon      sleeping  ...              root            /  2019-10-07T13:52:26.000Z
    1       396           iscsid      sleeping  ...              root            /  2019-10-07T13:52:27.000Z
    2       396           iscsid      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    3       396      ksoftirqd/0      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    4       396      kworker/0:1      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    ..      ...              ...           ...  ...               ...          ...                       ...
    25      961          polkitd      sleeping  ...              root            /  2019-10-07T12:21:31.000Z
    26      961        rcu_sched      sleeping  ...              root            /  2019-10-07T12:21:31.000Z
    27      961          systemd      sleeping  ...              root            /  2019-10-07T12:20:24.000Z
    28      961  systemd-journal      sleeping  ...              root            /  2019-10-07T12:20:38.000Z
    29      961  unattended-upgr      sleeping  ...              root            /  2019-10-07T12:21:26.000Z
    
    def dig(dct, path):
        ret = dct
        for step in path.split('.'):
            if step not in ret:
                print(f'**** No {step} element.')
                return None
            ret = ret[step]
            if isinstance(ret, list):
                ret = ret[0]
        return ret
    
    def dig2(dct, step, res, key='key'):
        dct = dig(dct, step + '.buckets')
        res.append(dct[key])
        return(dct)
    
    我已经尝试了几乎所有的方法,我用一个可怕的多循环代码来解析它,但我认为有一种更简单的方法可以做到这一点,但是这个社区中没有一篇帖子可以帮助我

    你能帮我弄到这样的东西吗

    我的结果(不完整):

        {
       "took":32,
       "timed_out":false,
       "_shards":{
          "total":4,
          "successful":4,
          "skipped":0,
          "failed":0
       },
       "hits":{
          "total":{
             "value":94,
             "relation":"eq"
          },
          "max_score":"None",
          "hits":[
    
          ]
       },
       "aggregations":{
          "hostname":{
             "doc_count_error_upper_bound":0,
             "sum_other_doc_count":0,
             "buckets":[
                {
                   "key":"396",
                   "doc_count":47,
                   "process_name":{
                      "doc_count_error_upper_bound":0,
                      "sum_other_doc_count":16,
                      "buckets":[
                         {
                            "key":"accounts-daemon",
                            "doc_count":2,
                            "process_state":{
                               "doc_count_error_upper_bound":0,
                               "sum_other_doc_count":0,
                               "buckets":[
                                  {
                                     "key":"sleeping",
                                     "doc_count":2,
                                     "process_pid":{
                                        "doc_count_error_upper_bound":0,
                                        "sum_other_doc_count":0,
                                        "buckets":[
                                           {
                                              "key":1092,
                                              "doc_count":2,
                                              "process_ppid":{
                                                 "doc_count_error_upper_bound":0,
                                                 "sum_other_doc_count":0,
                                                 "buckets":[
                                                    {
                                                       "key":1,
                                                       "doc_count":2,
                                                       "process_pgid":{
                                                          "doc_count_error_upper_bound":0,
                                                          "sum_other_doc_count":0,
                                                          "buckets":[
                                                             {
                                                                "key":1092,
                                                                "doc_count":2,
                                                                "process_cmdline":{
                                                                   "doc_count_error_upper_bound":0,
                                                                   "sum_other_doc_count":0,
                                                                   "buckets":[
                                                                      {
                                                                         "key":"/usr/lib/accountsservice/accounts-daemon",
                                                                         "doc_count":2,
                                                                         "process_username":{
                                                                            "doc_count_error_upper_bound":0,
                                                                            "sum_other_doc_count":0,
                                                                            "buckets":[
                                                                               {
                                                                                  "key":"root",
                                                                                  "doc_count":2,
                                                                                  "process_cwd":{
                                                                                     "doc_count_error_upper_bound":0,
                                                                                     "sum_other_doc_count":0,
                                                                                     "buckets":[
                                                                                        {
                                                                                           "key":"/",
                                                                                           "doc_count":2,
                                                                                           "process_cpu_start_time":{
                                                                                              "doc_count_error_upper_bound":0,
                                                                                              "sum_other_doc_count":0,
                                                                                              "buckets":[
                                                                                                 {
                                                                                                    "key":1570456346000,
                                                                                                    "key_as_string":"2019-10-07T13:52:26.000Z",
                                                                                                    "doc_count":2,
                                                                                                    "process_fd_limit_hard":{
                                                                                                       "value":4096.0
                                                                                                    },
                                                                                                    "process_fd_open":{
                                                                                                       "value":8.0
                                                                                                    },
                                                                                                    "process_memory_size":{
                                                                                                       "value":281055232.0
                                                                                                    },
                                                                                                    "process_memory_rss_bytes":{
                                                                                                       "value":6168576.0
                                                                                                    },
                                                                                                    "process_memory_share":{
                                                                                                       "value":5464064.0
                                                                                                    },
                                                                                                    "process_cpu_total_pct":{
                                                                                                       "value":0.0005
                                                                                                    },
                                                                                                    "process_fd_limit_soft":{
                                                                                                       "value":1024.0
                                                                                                    },
                                                                                                    "process_cpu_system_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_user_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_norm_pct":{
                                                                                                       "value":0.0005
                                                                                                    },
                                                                                                    "process_cpu_total_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_value":{
                                                                                                       "value":18775.0
                                                                                                    }
                                                                                                 }
                                                                                              ]
                                                                                           }
                                                                                        }
                                                                                     ]
                                                                                  }
                                                                               }
                                                                            ]
                                                                         }
                                                                      }
                                                                   ]
                                                                }
                                                             }
                                                          ]
                                                       }
                                                    }
                                                 ]
                                              }
                                           }
                                        ]
                                     }
                                  }
                               ]
                            }
                         }
                      ]
                   }
                },
                {
                   "key":"961",
                   "doc_count":47,
                   "process_name":{
                      "doc_count_error_upper_bound":0,
                      "sum_other_doc_count":16,
                      "buckets":[
                         {
                            "key":"accounts-daemon",
                            "doc_count":2,
                            "process_state":{
                               "doc_count_error_upper_bound":0,
                               "sum_other_doc_count":0,
                               "buckets":[
                                  {
                                     "key":"sleeping",
                                     "doc_count":2,
                                     "process_pid":{
                                        "doc_count_error_upper_bound":0,
                                        "sum_other_doc_count":0,
                                        "buckets":[
                                           {
                                              "key":1070,
                                              "doc_count":2,
                                              "process_ppid":{
                                                 "doc_count_error_upper_bound":0,
                                                 "sum_other_doc_count":0,
                                                 "buckets":[
                                                    {
                                                       "key":1,
                                                       "doc_count":2,
                                                       "process_pgid":{
                                                          "doc_count_error_upper_bound":0,
                                                          "sum_other_doc_count":0,
                                                          "buckets":[
                                                             {
                                                                "key":1070,
                                                                "doc_count":2,
                                                                "process_cmdline":{
                                                                   "doc_count_error_upper_bound":0,
                                                                   "sum_other_doc_count":0,
                                                                   "buckets":[
                                                                      {
                                                                         "key":"/usr/lib/accountsservice/accounts-daemon",
                                                                         "doc_count":2,
                                                                         "process_username":{
                                                                            "doc_count_error_upper_bound":0,
                                                                            "sum_other_doc_count":0,
                                                                            "buckets":[
                                                                               {
                                                                                  "key":"root",
                                                                                  "doc_count":2,
                                                                                  "process_cwd":{
                                                                                     "doc_count_error_upper_bound":0,
                                                                                     "sum_other_doc_count":0,
                                                                                     "buckets":[
                                                                                        {
                                                                                           "key":"/",
                                                                                           "doc_count":2,
                                                                                           "process_cpu_start_time":{
                                                                                              "doc_count_error_upper_bound":0,
                                                                                              "sum_other_doc_count":0,
                                                                                              "buckets":[
                                                                                                 {
                                                                                                    "key":1570450883000,
                                                                                                    "key_as_string":"2019-10-07T12:21:23.000Z",
                                                                                                    "doc_count":2,
                                                                                                    "process_fd_limit_hard":{
                                                                                                       "value":4096.0
                                                                                                    },
                                                                                                    "process_fd_open":{
                                                                                                       "value":8.0
                                                                                                    },
                                                                                                    "process_memory_size":{
                                                                                                       "value":281153536.0
                                                                                                    },
                                                                                                    "process_memory_rss_bytes":{
                                                                                                       "value":5992448.0
                                                                                                    },
                                                                                                    "process_memory_share":{
                                                                                                       "value":5279744.0
                                                                                                    },
                                                                                                    "process_cpu_total_pct":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_fd_limit_soft":{
                                                                                                       "value":1024.0
                                                                                                    },
                                                                                                    "process_cpu_system_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_user_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_norm_pct":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_value":{
                                                                                                       "value":13410.0
                                                                                                    }
                                                                                                 }
                                                                                              ]
                                                                                           }
                                                                                        }
                                                                                     ]
                                                                                  }
                                                                               }
                                                                            ]
                                                                         }
                                                                      }
                                                                   ]
                                                                }
                                                             }
                                                          ]
                                                       }
                                                    }
                                                 ]
                                              }
                                           }
                                        ]
                                     }
                                  }
                               ]
                            }
                         }
                      ]
                   }
                }
             ]
          }
       }
    }
    
       hostname     process_name process_state  ...  process_username  process_cwd    process_cpu_start_time
    0       396  accounts-daemon      sleeping  ...              root            /  2019-10-07T13:52:26.000Z
    1       396           iscsid      sleeping  ...              root            /  2019-10-07T13:52:27.000Z
    2       396           iscsid      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    3       396      ksoftirqd/0      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    4       396      kworker/0:1      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    ..      ...              ...           ...  ...               ...          ...                       ...
    25      961          polkitd      sleeping  ...              root            /  2019-10-07T12:21:31.000Z
    26      961        rcu_sched      sleeping  ...              root            /  2019-10-07T12:21:31.000Z
    27      961          systemd      sleeping  ...              root            /  2019-10-07T12:20:24.000Z
    28      961  systemd-journal      sleeping  ...              root            /  2019-10-07T12:20:38.000Z
    29      961  unattended-upgr      sleeping  ...              root            /  2019-10-07T12:21:26.000Z
    
    def dig(dct, path):
        ret = dct
        for step in path.split('.'):
            if step not in ret:
                print(f'**** No {step} element.')
                return None
            ret = ret[step]
            if isinstance(ret, list):
                ret = ret[0]
        return ret
    
    def dig2(dct, step, res, key='key'):
        dct = dig(dct, step + '.buckets')
        res.append(dct[key])
        return(dct)
    
    我知道这不容易,所以我会感谢你的帮助

    提前谢谢。

    这个主意 主要思想是向下导航JSON对象的结构。 为了使程序可读,我提出了两个函数,它们执行 此操作将导航并沿描述路径将键附加到列表中 (当前行的内容)

    函数向下导航JSON对象 功能1:

        {
       "took":32,
       "timed_out":false,
       "_shards":{
          "total":4,
          "successful":4,
          "skipped":0,
          "failed":0
       },
       "hits":{
          "total":{
             "value":94,
             "relation":"eq"
          },
          "max_score":"None",
          "hits":[
    
          ]
       },
       "aggregations":{
          "hostname":{
             "doc_count_error_upper_bound":0,
             "sum_other_doc_count":0,
             "buckets":[
                {
                   "key":"396",
                   "doc_count":47,
                   "process_name":{
                      "doc_count_error_upper_bound":0,
                      "sum_other_doc_count":16,
                      "buckets":[
                         {
                            "key":"accounts-daemon",
                            "doc_count":2,
                            "process_state":{
                               "doc_count_error_upper_bound":0,
                               "sum_other_doc_count":0,
                               "buckets":[
                                  {
                                     "key":"sleeping",
                                     "doc_count":2,
                                     "process_pid":{
                                        "doc_count_error_upper_bound":0,
                                        "sum_other_doc_count":0,
                                        "buckets":[
                                           {
                                              "key":1092,
                                              "doc_count":2,
                                              "process_ppid":{
                                                 "doc_count_error_upper_bound":0,
                                                 "sum_other_doc_count":0,
                                                 "buckets":[
                                                    {
                                                       "key":1,
                                                       "doc_count":2,
                                                       "process_pgid":{
                                                          "doc_count_error_upper_bound":0,
                                                          "sum_other_doc_count":0,
                                                          "buckets":[
                                                             {
                                                                "key":1092,
                                                                "doc_count":2,
                                                                "process_cmdline":{
                                                                   "doc_count_error_upper_bound":0,
                                                                   "sum_other_doc_count":0,
                                                                   "buckets":[
                                                                      {
                                                                         "key":"/usr/lib/accountsservice/accounts-daemon",
                                                                         "doc_count":2,
                                                                         "process_username":{
                                                                            "doc_count_error_upper_bound":0,
                                                                            "sum_other_doc_count":0,
                                                                            "buckets":[
                                                                               {
                                                                                  "key":"root",
                                                                                  "doc_count":2,
                                                                                  "process_cwd":{
                                                                                     "doc_count_error_upper_bound":0,
                                                                                     "sum_other_doc_count":0,
                                                                                     "buckets":[
                                                                                        {
                                                                                           "key":"/",
                                                                                           "doc_count":2,
                                                                                           "process_cpu_start_time":{
                                                                                              "doc_count_error_upper_bound":0,
                                                                                              "sum_other_doc_count":0,
                                                                                              "buckets":[
                                                                                                 {
                                                                                                    "key":1570456346000,
                                                                                                    "key_as_string":"2019-10-07T13:52:26.000Z",
                                                                                                    "doc_count":2,
                                                                                                    "process_fd_limit_hard":{
                                                                                                       "value":4096.0
                                                                                                    },
                                                                                                    "process_fd_open":{
                                                                                                       "value":8.0
                                                                                                    },
                                                                                                    "process_memory_size":{
                                                                                                       "value":281055232.0
                                                                                                    },
                                                                                                    "process_memory_rss_bytes":{
                                                                                                       "value":6168576.0
                                                                                                    },
                                                                                                    "process_memory_share":{
                                                                                                       "value":5464064.0
                                                                                                    },
                                                                                                    "process_cpu_total_pct":{
                                                                                                       "value":0.0005
                                                                                                    },
                                                                                                    "process_fd_limit_soft":{
                                                                                                       "value":1024.0
                                                                                                    },
                                                                                                    "process_cpu_system_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_user_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_norm_pct":{
                                                                                                       "value":0.0005
                                                                                                    },
                                                                                                    "process_cpu_total_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_value":{
                                                                                                       "value":18775.0
                                                                                                    }
                                                                                                 }
                                                                                              ]
                                                                                           }
                                                                                        }
                                                                                     ]
                                                                                  }
                                                                               }
                                                                            ]
                                                                         }
                                                                      }
                                                                   ]
                                                                }
                                                             }
                                                          ]
                                                       }
                                                    }
                                                 ]
                                              }
                                           }
                                        ]
                                     }
                                  }
                               ]
                            }
                         }
                      ]
                   }
                },
                {
                   "key":"961",
                   "doc_count":47,
                   "process_name":{
                      "doc_count_error_upper_bound":0,
                      "sum_other_doc_count":16,
                      "buckets":[
                         {
                            "key":"accounts-daemon",
                            "doc_count":2,
                            "process_state":{
                               "doc_count_error_upper_bound":0,
                               "sum_other_doc_count":0,
                               "buckets":[
                                  {
                                     "key":"sleeping",
                                     "doc_count":2,
                                     "process_pid":{
                                        "doc_count_error_upper_bound":0,
                                        "sum_other_doc_count":0,
                                        "buckets":[
                                           {
                                              "key":1070,
                                              "doc_count":2,
                                              "process_ppid":{
                                                 "doc_count_error_upper_bound":0,
                                                 "sum_other_doc_count":0,
                                                 "buckets":[
                                                    {
                                                       "key":1,
                                                       "doc_count":2,
                                                       "process_pgid":{
                                                          "doc_count_error_upper_bound":0,
                                                          "sum_other_doc_count":0,
                                                          "buckets":[
                                                             {
                                                                "key":1070,
                                                                "doc_count":2,
                                                                "process_cmdline":{
                                                                   "doc_count_error_upper_bound":0,
                                                                   "sum_other_doc_count":0,
                                                                   "buckets":[
                                                                      {
                                                                         "key":"/usr/lib/accountsservice/accounts-daemon",
                                                                         "doc_count":2,
                                                                         "process_username":{
                                                                            "doc_count_error_upper_bound":0,
                                                                            "sum_other_doc_count":0,
                                                                            "buckets":[
                                                                               {
                                                                                  "key":"root",
                                                                                  "doc_count":2,
                                                                                  "process_cwd":{
                                                                                     "doc_count_error_upper_bound":0,
                                                                                     "sum_other_doc_count":0,
                                                                                     "buckets":[
                                                                                        {
                                                                                           "key":"/",
                                                                                           "doc_count":2,
                                                                                           "process_cpu_start_time":{
                                                                                              "doc_count_error_upper_bound":0,
                                                                                              "sum_other_doc_count":0,
                                                                                              "buckets":[
                                                                                                 {
                                                                                                    "key":1570450883000,
                                                                                                    "key_as_string":"2019-10-07T12:21:23.000Z",
                                                                                                    "doc_count":2,
                                                                                                    "process_fd_limit_hard":{
                                                                                                       "value":4096.0
                                                                                                    },
                                                                                                    "process_fd_open":{
                                                                                                       "value":8.0
                                                                                                    },
                                                                                                    "process_memory_size":{
                                                                                                       "value":281153536.0
                                                                                                    },
                                                                                                    "process_memory_rss_bytes":{
                                                                                                       "value":5992448.0
                                                                                                    },
                                                                                                    "process_memory_share":{
                                                                                                       "value":5279744.0
                                                                                                    },
                                                                                                    "process_cpu_total_pct":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_fd_limit_soft":{
                                                                                                       "value":1024.0
                                                                                                    },
                                                                                                    "process_cpu_system_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_user_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_norm_pct":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_value":{
                                                                                                       "value":13410.0
                                                                                                    }
                                                                                                 }
                                                                                              ]
                                                                                           }
                                                                                        }
                                                                                     ]
                                                                                  }
                                                                               }
                                                                            ]
                                                                         }
                                                                      }
                                                                   ]
                                                                }
                                                             }
                                                          ]
                                                       }
                                                    }
                                                 ]
                                              }
                                           }
                                        ]
                                     }
                                  }
                               ]
                            }
                         }
                      ]
                   }
                }
             ]
          }
       }
    }
    
       hostname     process_name process_state  ...  process_username  process_cwd    process_cpu_start_time
    0       396  accounts-daemon      sleeping  ...              root            /  2019-10-07T13:52:26.000Z
    1       396           iscsid      sleeping  ...              root            /  2019-10-07T13:52:27.000Z
    2       396           iscsid      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    3       396      ksoftirqd/0      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    4       396      kworker/0:1      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    ..      ...              ...           ...  ...               ...          ...                       ...
    25      961          polkitd      sleeping  ...              root            /  2019-10-07T12:21:31.000Z
    26      961        rcu_sched      sleeping  ...              root            /  2019-10-07T12:21:31.000Z
    27      961          systemd      sleeping  ...              root            /  2019-10-07T12:20:24.000Z
    28      961  systemd-journal      sleeping  ...              root            /  2019-10-07T12:20:38.000Z
    29      961  unattended-upgr      sleeping  ...              root            /  2019-10-07T12:21:26.000Z
    
    def dig(dct, path):
        ret = dct
        for step in path.split('.'):
            if step not in ret:
                print(f'**** No {step} element.')
                return None
            ret = ret[step]
            if isinstance(ret, list):
                ret = ret[0]
        return ret
    
    def dig2(dct, step, res, key='key'):
        dct = dig(dct, step + '.buckets')
        res.append(dct[key])
        return(dct)
    
    这个函数“深入”dct——一个字典(JSON对象), 沿着路径(点分隔字符串)。 另一个细节是,如果目标子对象(函数 下降)是一个列表,然后还有额外的“下降”步骤,即 函数下降到此列表的第一个元素。 返回值是所有下降后的“最终”子对象

    功能2:

        {
       "took":32,
       "timed_out":false,
       "_shards":{
          "total":4,
          "successful":4,
          "skipped":0,
          "failed":0
       },
       "hits":{
          "total":{
             "value":94,
             "relation":"eq"
          },
          "max_score":"None",
          "hits":[
    
          ]
       },
       "aggregations":{
          "hostname":{
             "doc_count_error_upper_bound":0,
             "sum_other_doc_count":0,
             "buckets":[
                {
                   "key":"396",
                   "doc_count":47,
                   "process_name":{
                      "doc_count_error_upper_bound":0,
                      "sum_other_doc_count":16,
                      "buckets":[
                         {
                            "key":"accounts-daemon",
                            "doc_count":2,
                            "process_state":{
                               "doc_count_error_upper_bound":0,
                               "sum_other_doc_count":0,
                               "buckets":[
                                  {
                                     "key":"sleeping",
                                     "doc_count":2,
                                     "process_pid":{
                                        "doc_count_error_upper_bound":0,
                                        "sum_other_doc_count":0,
                                        "buckets":[
                                           {
                                              "key":1092,
                                              "doc_count":2,
                                              "process_ppid":{
                                                 "doc_count_error_upper_bound":0,
                                                 "sum_other_doc_count":0,
                                                 "buckets":[
                                                    {
                                                       "key":1,
                                                       "doc_count":2,
                                                       "process_pgid":{
                                                          "doc_count_error_upper_bound":0,
                                                          "sum_other_doc_count":0,
                                                          "buckets":[
                                                             {
                                                                "key":1092,
                                                                "doc_count":2,
                                                                "process_cmdline":{
                                                                   "doc_count_error_upper_bound":0,
                                                                   "sum_other_doc_count":0,
                                                                   "buckets":[
                                                                      {
                                                                         "key":"/usr/lib/accountsservice/accounts-daemon",
                                                                         "doc_count":2,
                                                                         "process_username":{
                                                                            "doc_count_error_upper_bound":0,
                                                                            "sum_other_doc_count":0,
                                                                            "buckets":[
                                                                               {
                                                                                  "key":"root",
                                                                                  "doc_count":2,
                                                                                  "process_cwd":{
                                                                                     "doc_count_error_upper_bound":0,
                                                                                     "sum_other_doc_count":0,
                                                                                     "buckets":[
                                                                                        {
                                                                                           "key":"/",
                                                                                           "doc_count":2,
                                                                                           "process_cpu_start_time":{
                                                                                              "doc_count_error_upper_bound":0,
                                                                                              "sum_other_doc_count":0,
                                                                                              "buckets":[
                                                                                                 {
                                                                                                    "key":1570456346000,
                                                                                                    "key_as_string":"2019-10-07T13:52:26.000Z",
                                                                                                    "doc_count":2,
                                                                                                    "process_fd_limit_hard":{
                                                                                                       "value":4096.0
                                                                                                    },
                                                                                                    "process_fd_open":{
                                                                                                       "value":8.0
                                                                                                    },
                                                                                                    "process_memory_size":{
                                                                                                       "value":281055232.0
                                                                                                    },
                                                                                                    "process_memory_rss_bytes":{
                                                                                                       "value":6168576.0
                                                                                                    },
                                                                                                    "process_memory_share":{
                                                                                                       "value":5464064.0
                                                                                                    },
                                                                                                    "process_cpu_total_pct":{
                                                                                                       "value":0.0005
                                                                                                    },
                                                                                                    "process_fd_limit_soft":{
                                                                                                       "value":1024.0
                                                                                                    },
                                                                                                    "process_cpu_system_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_user_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_norm_pct":{
                                                                                                       "value":0.0005
                                                                                                    },
                                                                                                    "process_cpu_total_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_value":{
                                                                                                       "value":18775.0
                                                                                                    }
                                                                                                 }
                                                                                              ]
                                                                                           }
                                                                                        }
                                                                                     ]
                                                                                  }
                                                                               }
                                                                            ]
                                                                         }
                                                                      }
                                                                   ]
                                                                }
                                                             }
                                                          ]
                                                       }
                                                    }
                                                 ]
                                              }
                                           }
                                        ]
                                     }
                                  }
                               ]
                            }
                         }
                      ]
                   }
                },
                {
                   "key":"961",
                   "doc_count":47,
                   "process_name":{
                      "doc_count_error_upper_bound":0,
                      "sum_other_doc_count":16,
                      "buckets":[
                         {
                            "key":"accounts-daemon",
                            "doc_count":2,
                            "process_state":{
                               "doc_count_error_upper_bound":0,
                               "sum_other_doc_count":0,
                               "buckets":[
                                  {
                                     "key":"sleeping",
                                     "doc_count":2,
                                     "process_pid":{
                                        "doc_count_error_upper_bound":0,
                                        "sum_other_doc_count":0,
                                        "buckets":[
                                           {
                                              "key":1070,
                                              "doc_count":2,
                                              "process_ppid":{
                                                 "doc_count_error_upper_bound":0,
                                                 "sum_other_doc_count":0,
                                                 "buckets":[
                                                    {
                                                       "key":1,
                                                       "doc_count":2,
                                                       "process_pgid":{
                                                          "doc_count_error_upper_bound":0,
                                                          "sum_other_doc_count":0,
                                                          "buckets":[
                                                             {
                                                                "key":1070,
                                                                "doc_count":2,
                                                                "process_cmdline":{
                                                                   "doc_count_error_upper_bound":0,
                                                                   "sum_other_doc_count":0,
                                                                   "buckets":[
                                                                      {
                                                                         "key":"/usr/lib/accountsservice/accounts-daemon",
                                                                         "doc_count":2,
                                                                         "process_username":{
                                                                            "doc_count_error_upper_bound":0,
                                                                            "sum_other_doc_count":0,
                                                                            "buckets":[
                                                                               {
                                                                                  "key":"root",
                                                                                  "doc_count":2,
                                                                                  "process_cwd":{
                                                                                     "doc_count_error_upper_bound":0,
                                                                                     "sum_other_doc_count":0,
                                                                                     "buckets":[
                                                                                        {
                                                                                           "key":"/",
                                                                                           "doc_count":2,
                                                                                           "process_cpu_start_time":{
                                                                                              "doc_count_error_upper_bound":0,
                                                                                              "sum_other_doc_count":0,
                                                                                              "buckets":[
                                                                                                 {
                                                                                                    "key":1570450883000,
                                                                                                    "key_as_string":"2019-10-07T12:21:23.000Z",
                                                                                                    "doc_count":2,
                                                                                                    "process_fd_limit_hard":{
                                                                                                       "value":4096.0
                                                                                                    },
                                                                                                    "process_fd_open":{
                                                                                                       "value":8.0
                                                                                                    },
                                                                                                    "process_memory_size":{
                                                                                                       "value":281153536.0
                                                                                                    },
                                                                                                    "process_memory_rss_bytes":{
                                                                                                       "value":5992448.0
                                                                                                    },
                                                                                                    "process_memory_share":{
                                                                                                       "value":5279744.0
                                                                                                    },
                                                                                                    "process_cpu_total_pct":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_fd_limit_soft":{
                                                                                                       "value":1024.0
                                                                                                    },
                                                                                                    "process_cpu_system_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_user_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_norm_pct":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_ticks":{
                                                                                                       "value":0.0
                                                                                                    },
                                                                                                    "process_cpu_total_value":{
                                                                                                       "value":13410.0
                                                                                                    }
                                                                                                 }
                                                                                              ]
                                                                                           }
                                                                                        }
                                                                                     ]
                                                                                  }
                                                                               }
                                                                            ]
                                                                         }
                                                                      }
                                                                   ]
                                                                }
                                                             }
                                                          ]
                                                       }
                                                    }
                                                 ]
                                              }
                                           }
                                        ]
                                     }
                                  }
                               ]
                            }
                         }
                      ]
                   }
                }
             ]
          }
       }
    }
    
       hostname     process_name process_state  ...  process_username  process_cwd    process_cpu_start_time
    0       396  accounts-daemon      sleeping  ...              root            /  2019-10-07T13:52:26.000Z
    1       396           iscsid      sleeping  ...              root            /  2019-10-07T13:52:27.000Z
    2       396           iscsid      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    3       396      ksoftirqd/0      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    4       396      kworker/0:1      sleeping  ...              root            /  2019-10-07T13:52:28.000Z
    ..      ...              ...           ...  ...               ...          ...                       ...
    25      961          polkitd      sleeping  ...              root            /  2019-10-07T12:21:31.000Z
    26      961        rcu_sched      sleeping  ...              root            /  2019-10-07T12:21:31.000Z
    27      961          systemd      sleeping  ...              root            /  2019-10-07T12:20:24.000Z
    28      961  systemd-journal      sleeping  ...              root            /  2019-10-07T12:20:38.000Z
    29      961  unattended-upgr      sleeping  ...              root            /  2019-10-07T12:21:26.000Z
    
    def dig(dct, path):
        ret = dct
        for step in path.split('.'):
            if step not in ret:
                print(f'**** No {step} element.')
                return None
            ret = ret[step]
            if isinstance(ret, list):
                ret = ret[0]
        return ret
    
    def dig2(dct, step, res, key='key'):
        dct = dig(dct, step + '.buckets')
        res.append(dct[key])
        return(dct)
    
    参数:

    • dct-源字典
    • 步骤-下降路径的第一步
    • res-结果列表,在下降后向其追加键
    • key—要从目标子对象读取的键的名称
    此功能可加快向下导航源对象的速度。您只指定 第一步,此函数下降一步(到铲斗)。 然后,res将附加在此级别上找到的键

    返回值也是“最终”子对象,用于执行 进一步下降

    读取源JSON文件 要读取您的JSON字符串,我将其保存在一个文件中,如下所示:

    with open('input.json') as json_file:
        data = json.load(json_file)
    
    steps = ['hostname', 'process_name', 'process_state', 'process_pid',
        'process_ppid', 'process_pgid', 'process_cmdline',
        'process_username', 'process_cwd', 'process_cpu_start_time']
    
    步骤清单 向下导航源对象的步骤列表如下所示:

    with open('input.json') as json_file:
        data = json.load(json_file)
    
    steps = ['hostname', 'process_name', 'process_state', 'process_pid',
        'process_ppid', 'process_pgid', 'process_cmdline',
        'process_username', 'process_cwd', 'process_cpu_start_time']
    
    此列表也是结果数据框中的列名列表

    主程序 首先,我们创建tbl—一个空列表,其中包含后续行的数据 将被收集

    那么,对于巴克来说。。。对aggregations.hostname中的每个bucket进行操作

    当前行的结果的第一个元素是来自当前bucket的key

    之后,对于步骤[1:-1]中的步骤,深入到以下步骤,不包括最后一步, 并将每个键添加到结果列表中

    下降的最后一步在这个循环之外,因为这一次 append是key_as_string,最后是res(当前行的数据) 已添加到tbl中

    循环之后,将创建结果数据帧

    由于文章中包含的源JSON对象只包含2个bucket, 此数据帧仅包含2行