0

I'm new to python

Below is my output from cloudwatchinsights query

{'results': [[{'field': '@m', 'value': 'upload  `onetest.csv`'}, {'field': '@ptr', 'value': 'CmkKMAosMDI2uwxIuRlQtBQQAhgB'}], [{'field': '@m', 'value': 'upload `onecube.csv`'}, {'field': '@ptr', 'value': 'CmkKMAosMDI2NzYzlQuxQQAhgB'}], [{'field': '@m', 'value': 'upload  `onetra.csv`'}, {'field': '@ptr', 'value': 'CmkKMAosyhRIghxQ/BYQBhgB'}], [{'field': '@m', 'value': 'upload  `onecant.csv`'}], 'statistics': {'recordsMatched': 79.0, 'recordsScanned': 550.0, 'bytesScanned': 147117.0}, 'status': 'Complete', 'ResponseMetadata': {'RequestId': '7c79a28597ca', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '7c7d498a9a28597ca', 'content-type': 'application/x-amz-json-1.1', 'content-length': '20282', 'date': 'Tue, 17:17:11 GMT'}, 'RetryAttempts': 0}}

I would like to parse the output and expected as below using python

1. onetest.csv.
2. onecube.csv
3.onetra.csv
4.onecant.csv

Ive tried using regular expression using re module, But I could get the exact output.

syid
  • 5
  • 4
  • Does this answer your question? [How to parse data in JSON format?](https://stackoverflow.com/questions/7771011/how-to-parse-data-in-json-format) – Yoshikage Kira May 18 '21 at 16:41
  • Ive tried using json.loads but it throws an exception `{ "errorMessage": "the JSON object must be str, bytes or bytearray, not NoneType", "errorType": "TypeError", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 37, in lambda_handler\n json_object = json.loads(content)\n", " File \"/var/lang/lib/python3.8/json/__init__.py\", line 341, in loads\n raise TypeError(f'the JSON object must be str, bytes or bytearray, '\n" ] }` – syid May 18 '21 at 16:45

1 Answers1

1

Try this way.

cloudwatchinsights = [[{'field': '@m', 'value': 'upload  `onecube.csv`'}, {'field': '@ptr', 'value': 'CmoKMDAYAQ=='}], [{'field': '@m', 'value': 'upload  `onetra.csv`'}, {'field': '@ptr', 'value': 'CmoKMAI5JECoYAQ=='}]]

result = [ each[0]['value'][9:-1] for each in cloudwatchinsights ]

Output:

['onecube.csv', 'onetra.csv']
top talent
  • 615
  • 4
  • 17
  • im getting below exception ```{ "errorMessage": "string indices must be integers", "errorType": "TypeError", "stackTrace": [ " File \"/var/task/lambda_function.py\", line 37, in lambda_handler\n result = [each[0]['value'][9:-1] for each in response]\n", " File \"/var/task/lambda_function.py\", line 37, in \n result = [each[0]['value'][9:-1] for each in response]\n" ] }``` – syid May 18 '21 at 17:02
  • Hi Ive updated the complete response when I execute the query – syid May 18 '21 at 17:31