简体   繁体   English

使用python从json文件中提取数据

[英]extract data from json file using python

I have this json file.我有这个 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"
    
    
  }
  
}

I need to extract entityId": "PROCESS_1234" and "properties": { "detectedName": "Windows System" as a data frame. The data frame needs to look like this:我需要提取 entityId": "PROCESS_1234" 和 "properties": { "detectedName": "Windows System" 作为数据框。数据框需要如下所示:

entityId        detectedName
PROCESS_1234    Windows System

I have tried this:我试过这个:

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

I get this error:我收到此错误:

---------------------------------------------------------------------------
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

To extract entityId , do:要提取entityId ,请执行以下操作:

print(resp2['entityId'])

To extract detectedName , do:要提取detectedName名称,请执行以下操作:

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

this error occur when you pass a string value传递字符串值时会发生此错误

print(resp2[0][1]) try like this. print(resp2[0][1]) 试试这样。

The program该程序

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)

The output输出

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