简体   繁体   中英

How do I import a JSON file which has multiple keys in Python?

For example: I have a program that generates usage logs like this in a JSON file. The JSON file log contains a lot of the same key called "activity" like the following:

  "probe": "PROCESS_PROBE",
  "status": "ProcessCreated",
  "processName": "backgroundTaskHost.exe",
  "path": "C:\\WINDOWS\\system32\\backgroundTaskHost.exe",
  "creationClassName": "Win32_Process",
  "handle": "21632",
  "priority": "Normal",
  "commandLine": "\"C:\\WINDOWS\\system32\\backgroundTaskHost.exe\" -ServerName:CortanaUI.AppXy7vb4pc2dr3kc93kfc509b1d0arkfb2x.mca",
  "handleCount": 236,
  "processId": 21632,
  "parentProcessId": 112,
  "pageFileUsage": 4244,
  "creationDate": "20200410172922.614702+120",
  "annotations": {
    "userName": "datta",
    "timeSinceStartup": 259878750,
    "ticksOfEvent": 637221365629757593
  }
},
"activity":{
  "probe": "PROCESS_PROBE",
  "status": "ProcessDeleted",
  "processName": "RuntimeBroker.exe",
  "path": "C:\\Windows\\System32\\RuntimeBroker.exe",
  "creationClassName": "Win32_Process",
  "handle": "8504",
  "priority": "Normal",
  "handleCount": 285,
  "processId": 8504,
  "parentProcessId": 112,
  "pageFileUsage": 3180,
  "creationDate": "20200410172757.934567+120",
  "terminationDate": null,
  "annotations": {
    "userName": "datta",
    "timeSinceStartup": 259883953,
    "ticksOfEvent": 637221365681937472
  }
},
"activity":{
  "probe": "FILERESOURCE_PROBE",
  "status": "Changed",
  "path": "C:\\Users\\datta\\eclipse\\jee-2019-12",
  "entityName": "eclipse",
  "extension": "",
  "attributes": "Directory",
  "owner": "null",
  "length": 0,
  "isReadOnly": false,
  "creationTime": "2020-01-17T09:42:08.5092897+01:00",
  "lastWriteTime": "2020-03-25T10:56:10.7382329+01:00",
  "lastAccessTime": "2020-04-10T17:29:29.9811767+02:00",
  "annotations": {
    "userName": "datta",
    "timeSinceStartup": 259885750,
    "ticksOfEvent": 637221365699837331
  }
},
"activity":{
  "probe": "FILERESOURCE_PROBE",
  "status": "Changed",
  "path": "C:\\Users\\datta\\eclipse",
  "entityName": "jee-2019-12",
  "extension": "",
  "attributes": "Directory",
  "owner": "null",
  "length": 0,
  "isReadOnly": false,
  "creationTime": "2020-01-17T09:42:08.5083+01:00",
  "lastWriteTime": "2020-01-17T09:42:08.5092897+01:00",
  "lastAccessTime": "2020-04-10T17:29:29.9801436+02:00",
  "annotations": {
    "userName": "datta",
    "timeSinceStartup": 259885750,
    "ticksOfEvent": 637221365699906960
  }
},
"activity":{
  "probe": "FILERESOURCE_PROBE",
  "status": "Changed",
  "path": "C:\\Users\\datta",
  "entityName": "eclipse",
  "extension": "",
  "attributes": "Directory",
  "owner": "null",
  "length": 0,
  "isReadOnly": false,
  "creationTime": "2020-01-17T09:42:08.5083+01:00",
  "lastWriteTime": "2020-01-17T09:42:08.5083+01:00",
  "lastAccessTime": "2020-04-10T17:29:29.9922013+02:00",
  "annotations": {
    "userName": "datta",
    "timeSinceStartup": 259885765,
    "ticksOfEvent": 637221365699922013
  }
}
}

I would like to load them into a python program. Currently, I am using logData = json.load(logfile) to load it but the problem is when I do so, it returns me a python dict with only the last "activity" key and the rest of the "activity" keys get over-written. I don't know how to load all of them. I would appreciate if you guys can help me with that. Thank you.

Having an object using different content for the same key looks odd, it'd probably make more sense to get an array of object in that case. But assuming you cannot control that, you must do "something else" with the incoming data and not not unpack key/value pairs into mapping objects. You can tell json.load() how to treat these pairs by registering object_pairs_hook to which the pair is passed and what it returns is becomes the corresponding python object. For instance:

logdata = json.load(logfile, object_pairs_hook=tuple)

And instead of a dict s (which would have been the default) you end up with tuple s of (key, value) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM