Извлечение данных из вложенных JSON с python 3 - PullRequest
1 голос
/ 26 марта 2020

Ищем способ извлечь данные из вложенного json. Данные были извлечены из планировщика windows как xml и преобразованы в json. Он представляет запланированные задачи ОС, около 100 задач, и моя цель - извлечь информацию из json во внешнюю БД. Вот как выглядит мой json файл:

{
    "Tasks": {
        "Task": [
            {
                "RegistrationInfo": {
                    "Author": "Adobe Systems Incorporated",
                    "Description": "This task keeps your Adobe Reader and Acrobat applications up to date with the latest enhancements and security fixes",
                    "URI": "\\Adobe Acrobat Update Task"
                },
                "Principals": {
                    "Principal": {
                        "GroupId": "S-1-5-4",
                        "_id": "Author"
                    }
                },
                "Settings": {
                    "DisallowStartIfOnBatteries": "true",
                    "StopIfGoingOnBatteries": "true",
                    "MultipleInstancesPolicy": "IgnoreNew",
                    "StartWhenAvailable": "true",
                    "IdleSettings": {
                        "Duration": "PT10M",
                        "WaitTimeout": "PT1H",
                        "StopOnIdleEnd": "true",
                        "RestartOnIdle": "false"
                    }
                },
                "Triggers": {
                    "LogonTrigger": {
                        "StartBoundary": "2013-08-01T12:05:00",
                        "EndBoundary": "2027-05-02T08:00:00",
                        "Delay": "PT12M",
                        "Repetition": {
                            "Interval": "PT3H30M"
                        },
                        "_id": "TriggerUserLoggon"
                    },
                    "CalendarTrigger": {
                        "StartBoundary": "2013-01-01T09:00:00",
                        "EndBoundary": "2027-05-02T12:05:00",
                        "ScheduleByDay": {
                            "DaysInterval": "1"
                        },
                        "_id": "TriggerDaily"
                    }
                },
                "Actions": {
                    "Exec": {
                        "Command": "C:\\Program Files (x86)\\Common Files\\Adobe\\ARM\\1.0\\AdobeARM.exe"
                    },
                    "_Context": "Author"
                },
                "_xmlns": "http://schemas.microsoft.com/windows/2004/02/mit/task",
                "_version": "1.2"
            },
            {
                "RegistrationInfo": {
                    "Author": "Adobe",
                    "Description": "This task keeps your Adobe Flash NPAPI Player installation up to date with the latest enhancements and security fixes. If this task is disabled or removed, Adobe Flash Player will be unable to automatically secure your machine with the latest security fixes.",
                    "URI": "\\Adobe Flash Player NPAPI Notifier"
                },
                "Principals": {
                    "Principal": {
                        "UserId": "S-1-5-21-2755204513-1269241785-1912306605-1001",
                        "LogonType": "InteractiveToken",
                        "_id": "Author"
                    }
                },
                "Settings": {
                    "DisallowStartIfOnBatteries": "false",
                    "StopIfGoingOnBatteries": "true",
                    "MultipleInstancesPolicy": "IgnoreNew",
                    "StartWhenAvailable": "true",
                    "RunOnlyIfNetworkAvailable": "true",
                    "IdleSettings": {
                        "Duration": "PT10M",
                        "WaitTimeout": "PT1H",
                        "StopOnIdleEnd": "true",
                        "RestartOnIdle": "false"
                    }
                },
                "Triggers": {
                    "CalendarTrigger": {
                        "StartBoundary": "1999-12-31T16:14:00-08:00",
                        "Repetition": {
                            "Interval": "PT1H",
                            "Duration": "P1D"
                        },
                        "ScheduleByDay": {
                            "DaysInterval": "7"
                        },
                        "_id": "NotificationTrigger"
                    }
                },
                "Actions": {
                    "Exec": {
                        "Command": "C:\\Windows\\SysWOW64\\Macromed\\Flash\\FlashUtil32_32_0_0_330_Plugin.exe",
                        "Arguments": "-check plugin"
                    },
                    "_Context": "Author"
                },
                "_xmlns": "http://schemas.microsoft.com/windows/2004/02/mit/task",
                "_version": "1.2"
            }

Приведенный ниже код работает только для одной задачи, но мне нужно получать данные по каждой задаче, думаю, мне нужно найти способ вставить количество задач к значению индекса и l oop их одно за другим, но ничего, что я пробую, не работает.

import json
index=1

with open('name.json', 'r') as f:
    array = json.load(f)

ts=(array['Tasks'])
print('Author is',ts['Task'][index]['RegistrationInfo']['Author'])
print('Description is' ,ts['Task'][index]['RegistrationInfo']['Description'])
print('URI is',ts['Task'][index]['RegistrationInfo']['URI'])
print('user ID is', ts['Task'][index]['Principals']['Principal']['UserId'])
print('Logon Type  is', ts['Task'][index]['Principals']['Principal']['LogonType'])
print(' ID is', ts['Task'][index]['Principals']['Principal']['_id'])
print(' DisallowStartIfOnBatteries is ', ts['Task'][index]['Settings']['DisallowStartIfOnBatteries'])
print(' StopIfGoingOnBatteries is ', ts['Task'][index]['Settings']['StopIfGoingOnBatteries'])
print(' MultipleInstancesPolicy is ', ts['Task'][index]['Settings']['MultipleInstancesPolicy'])
print(' StartWhenAvailable is ', ts['Task'][index]['Settings']['StartWhenAvailable'])
print(' RunOnlyIfNetworkAvailable is ', ts['Task'][index]['Settings']['RunOnlyIfNetworkAvailable'])
...