'extract data from json file using python
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
Solution 1:[1]
To extract entityId, do:
print(resp2['entityId'])
To extract detectedName, do:
print(resp2['properties']['detectedName'])
Solution 2:[2]
this error occur when you pass a string value
print(resp2[0][1]) try like this.
Solution 3:[3]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Avinash |
| Solution 2 | Jahan Zabe |
| Solution 3 | Gabriel Pellegrino |
