Home > Software engineering >  extract data from json file using python
extract data from json file using python

Time:05-24

I have this json file.

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

CodePudding user response:

To extract entityId, do:

print(resp2['entityId'])

To extract detectedName, do:

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

CodePudding user response:

this error occur when you pass a string value

print(resp2[0][1]) try like this.

CodePudding user response:

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