簡體   English   中英

使用python從json文件中提取數據

[英]extract data from json file using python

我有這個 json 文件。

{
  "entityId": "PROCESS_1234",
  "displayName": "Windows System",
  "firstSeenTms": 1619147697131,
  "lastSeenTms": 1653317760000,
  "properties": {
    "detectedName": "Windows System",
    "bitness": "32",
    "metadata": [],
    "awsNameTag": "Windows System",
    "softwareTechnologies": [
      {
        "type": "WINDOWS_SYSTEM"
      }
    ],
    "processType": "WINDOWS_SYSTEM"
    
    
  }
  
}

我需要提取 entityId": "PROCESS_1234" 和 "properties": { "detectedName": "Windows System" 作為數據框。數據框需要如下所示:

entityId        detectedName
PROCESS_1234    Windows System

我試過這個:

print(resp2['entityId']['properties'][0]['detectedName'])

我收到此錯誤:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-09b87f04b95e> in <module>
----> 1 print(resp2['entityId']['properties'][0]['detectedName'])

TypeError: string indices must be integers

要提取entityId ,請執行以下操作:

print(resp2['entityId'])

要提取detectedName名稱,請執行以下操作:

print(resp2['properties']['detectedName'])

傳遞字符串值時會發生此錯誤

print(resp2[0][1]) 試試這樣。

該程序

import pandas as pd
data = {
  "entityId": "PROCESS_1234",
  "displayName": "Windows System",
  "firstSeenTms": 1619147697131,
  "lastSeenTms": 1653317760000,
  "properties": {
    "detectedName": "Windows System",
    "bitness": "32",
    "metadata": [],
    "awsNameTag": "Windows System",
    "softwareTechnologies": [
      {
        "type": "WINDOWS_SYSTEM"
      }
    ],
    "processType": "WINDOWS_SYSTEM"
  }
}

rows = [(data['entityId'], data['properties']['detectedName'])]
x = pd.DataFrame(data=rows, columns=['entityId', 'detectedName'])
print(x)

輸出

bash-5.1$ python3 c.py
       entityId    detectedName
0  PROCESS_1234  Windows System

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM