[Solved] Schrodingers JSON – when working with a json doc it is erroring as both a list and a dict


As @Hitobat mentioned in commend – you have list with dictionary inside so you have to use [0] to get this dictionary. Or you have to use for-loop if you have more elements on list

data = [{'_id': '5f563c1bf8eaa9d98eca231f', 'allEnabledDs': None, 'allEnabledIdSor': None, 'correlationFilterEntitySource': True, 'created_at': '2020-09-07T13:56:43.469Z', 'dsConnectionList': None, 'folderToLabelMapping': None, 'idConnectionList': None, 'identityResolutionScan': False, 'info': None, 'isCustomScanProfile': None, 'modelId': None, 'name': 'Identity Discovery Scan', 'origin': 'Identity Discovery Scan', 'scan_progress_status': {'Started': '2020-09-07T13:56:43.469Z'}, 'shouldCreateClassifiers': None, 'skipIdScan': None, 'state': 'Started', 'type': 'identityDiscoveryScan', 'updated_at': '2020-09-07T16:59:45.294Z', 'identities_scanned': 0, 'correlation_completed': True, 'correlation_completed_dt': '2020-09-07T13:56:43.547Z', 'piisummary_completed_dt': '2020-09-07T13:56:43.642Z', 'stopRequested': True}]

print( data[0]['state'] )

for item in data:
    print( item['state'] )

Next time you can use type() to check what you have –

print( type(data) )

if it list then you can test length and/or check first element

print( len(data) )
print( type( data[0] ) )

if it is dict then you can check what keys you can use

print( data[0].keys() )

This way you can recognize how to get expected element(s).

You can also use json to format it with indentations and see how it looks like

import json

print( json.dumps(data, indent=2) )

Results:

[
  {
    "_id": "5f563c1bf8eaa9d98eca231f",
    "allEnabledDs": null,
    "allEnabledIdSor": null,
    "correlationFilterEntitySource": true,
    "created_at": "2020-09-07T13:56:43.469Z",
    "dsConnectionList": null,
    "folderToLabelMapping": null,
    "idConnectionList": null,
    "identityResolutionScan": false,
    "info": null,
    "isCustomScanProfile": null,
    "modelId": null,
    "name": "Identity Discovery Scan",
    "origin": "Identity Discovery Scan",
    "scan_progress_status": {
      "Started": "2020-09-07T13:56:43.469Z"
    },
    "shouldCreateClassifiers": null,
    "skipIdScan": null,
    "state": "Started",
    "type": "identityDiscoveryScan",
    "updated_at": "2020-09-07T16:59:45.294Z",
    "identities_scanned": 0,
    "correlation_completed": true,
    "correlation_completed_dt": "2020-09-07T13:56:43.547Z",
    "piisummary_completed_dt": "2020-09-07T13:56:43.642Z",
    "stopRequested": true
  }
]

Similar way you can use pprint (Pretty Print)

import pprint

pprint.pprint(data)

Result:

[{'_id': '5f563c1bf8eaa9d98eca231f',
  'allEnabledDs': None,
  'allEnabledIdSor': None,
  'correlationFilterEntitySource': True,
  'correlation_completed': True,
  'correlation_completed_dt': '2020-09-07T13:56:43.547Z',
  'created_at': '2020-09-07T13:56:43.469Z',
  'dsConnectionList': None,
  'folderToLabelMapping': None,
  'idConnectionList': None,
  'identities_scanned': 0,
  'identityResolutionScan': False,
  'info': None,
  'isCustomScanProfile': None,
  'modelId': None,
  'name': 'Identity Discovery Scan',
  'origin': 'Identity Discovery Scan',
  'piisummary_completed_dt': '2020-09-07T13:56:43.642Z',
  'scan_progress_status': {'Started': '2020-09-07T13:56:43.469Z'},
  'shouldCreateClassifiers': None,
  'skipIdScan': None,
  'state': 'Started',
  'stopRequested': True,
  'type': 'identityDiscoveryScan',
  'updated_at': '2020-09-07T16:59:45.294Z'}]

solved Schrodingers JSON – when working with a json doc it is erroring as both a list and a dict