1

I'm having trouble parsing the below JSON Response Dict object to just return/print the 'data' value (testing.test.com). See the dict below:

[{'_id': '~1742209152', 'id': '~1742209152', 'createdBy': 'test@test.com', 
'createdAt': 1666089754558, '_type': 'case_artifact', 'dataType': 'domain', 'data': 
'testing.test.com', 'startDate': 1666089754558, 'tlp': 2, 'pap': 2, 'tags': 
['Domain'], 'ioc': True, 'sighted': True, 'message': '', 'reports': {}, 'stats': {}, 
'ignoreSimilarity': False}]

Whenever I go to run the following code to attempt to parse the data, I'm shown an error 'print(observables['data'])TypeError: list indices must be integers or slices, not str':

    observables = json.dumps(response) #getting JSON response dict which works fine
    print(observables) #printing is successful

    print(observables['data']) #issue is here

I realise the error is suggesting I use int rather than string, but when I try to reverse this, it doesn't work and sends me on an endless number of errors. Is there a specific way to do this? I'm not overly confident in my scripting abilities so appreciate any pointers!

Ps - as a side note, this interaction is occurring been an API and my python file, but since I'm only having issues with the JSON response return parsing, I doubt that has any impact.

1 Answers1

2

your response is a list of dict objects. note that the first opening brackets are [ and not {.

you need to address the first (and only) object in your example and then access it as a dict using the 'data' key.

try print(observables[0]['data'])

EDIT: after seeing more of the code in chat room, and figuring out you used dumps() on the response object. I figured out that you're handling a string, and not a list/dict.

you should remove the call to dumps() since you're getting a requests.Response object as a response, you can just do: observables = response.json() and then continue with the original answer.

Daniel
  • 1,895
  • 9
  • 20
  • Thank you for replying. I tried as you suggested, but now i seem to be getting error: 'raise TypeError(f'Object of type {o.__class__.__name__} 'TypeError: Object of type Response is not JSON serializable'. I suspect this might be a different problem now. – EarthlyBale11 Nov 17 '22 at 09:31
  • please share your code, and if you can note where the exception is happening – Daniel Nov 17 '22 at 10:52
  • Bulk of the code: ` def run(self): Responder.run(self) case_id = self.get_param('data.id', None, 'CaseId is missing') query = And(Eq('dataType', 'domain')) response = api.get_case_observables(case_id, query=query) observables = json.dumps(response) #this is the initial dict response which returns fine having tested print(observables[0]['data']) #here is where the exception occurs if __name__ == "__main__": switchreport().run()` #name of the class which is not included above but in the code I've removed - works – EarthlyBale11 Nov 17 '22 at 11:21
  • let's continue the discussion in a [chat room](https://chat.stackoverflow.com/rooms/249696/daniels-room) – Daniel Nov 17 '22 at 11:43
  • @EarthlyBale11 I gave you write permission in the room. regardless, can you add the code from the comment to the original question as an edit below the original question? easier to understand the code with the markdown formatting. – Daniel Nov 17 '22 at 13:58
  • @EarthlyBale11 please see the new messages in the chat room. – Daniel Nov 20 '22 at 11:29