-1

I'm trying to extract the values from the following json code in this format by displaying each device in a different row. Any ideas on how to do it? Thanks in advance

device1 : 232323 Completed
device2 : 345848 Completed etc

I tried the below which gives me the following output:

[{'cid': 631084346, 'data': [[{'text': 'device1'}], [{'text': '732536:Completed.'}], [{'text': '1'}]], 'id': 1750501182}, {'cid': 1891684718, 'data': [[{'text': 'device2'}], [{'text': '732536:Completed.'}], [{'text': '1'}]], 'id': 2218910703}
api_readable = api_response.json()
    computer_name = str(api_readable['data']['result_sets'][0]['rows'])
   # computer_name = re.sub("[{},':]", "", computer_name)
    print(computer_name)

Sample Json used

{
    "data": {
        "max_available_age": "",
        "now": "2020/01/09 22:43:58 GMT-0000",
        "result_sets": [{
            "age": 0,
            "archived_question_id": 0,
            "columns": [{
                    "hash": 3409330187,
                    "name": "Computer Name",
                    "type": 1
                },
                {
                    "hash": 1792443391,
                    "name": "Action Statuses",
                    "type": 1
                },
                {
                    "hash": 0,
                    "name": "Count",
                    "type": 3
                }
            ],
            "error_count": 0,
            "estimated_total": 129,
            "expire_seconds": 3660,
            "filtered_row_count": 11,
            "filtered_row_count_machines": 11,
            "id": 2880628,
            "issue_seconds": 0,
            "item_count": 11,
            "mr_passed": 129,
            "mr_tested": 129,
            "no_results_count": 0,
            "passed": 128,
            "question_id": 2880628,
            "report_count": 233,
            "row_count": 11,
            "row_count_machines": 11,
            "rows": [{
                        "cid": 631084346,
                        "data": [
                            [{
                                "text": "device1"
                            }],
                            [{
                                "text": "732536:Completed."
                            }],
                            [{
                                "text": "1"
                            }]
                        ],
                        "id": 1750501182
                    },
                    {
                        "cid": 1891684718,
                        "data": [
                            [{
                                "text": "device2"
                            }],
                            [{
                                "text": "732536:Completed."
                            }],
                            [{
                                "text": "1"
                            }]
                        ],
                        "id": 2218910703
                    }
Barmar
  • 741,623
  • 53
  • 500
  • 612

1 Answers1

0

api_readable['data']['result_sets'][0]['rows'] is a list, you can simply loop over it and print the items you want.

for row in api_readable['data']['result_sets'][0]['rows']:
    print(f"{row['data'][0][0]['text']} : {row['data'][1][0]['text']}");
Barmar
  • 741,623
  • 53
  • 500
  • 612