2017-02-07 3 views
0

Я пытаюсь сделать некоторые генерации кода из данных JSON. Я получаю некоторые данные резервного копирования AWS в файл JSON. Я беру первую запись и создаю json-схему из одной строки.Traverse JSON Schema in Powershell

Когда я загружаю схему в Powershell, я не могу перечислять свойства, потому что они десериализуются как первичные свойства, а не как массив в «свойствах». Это похоже на объект json schema, где список свойств отображается как список объектов, а не массив, но я не уверен.

Я предполагаю, что анализатор PowerShell JSON не может видеть массив там, поэтому просто делает их свойствами.

Я действительно не хочу вручную перемещаться по схеме JSON, чтобы получить список свойств и их типы.

schema.json

{ 
    "$schema": "http://json-schema.org/draft-04/schema#", 
    "description": "", 
    "type": "object", 
    "properties": { 
    "definition": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    }, 
    "calcDefFilters": { 
     "type": "object", 
     "properties": { 
     "l": { 
      "type": "array", 
      "uniqueItems": true, 
      "minItems": 1, 
      "items": { 
      "required": [], 
      "properties": { 
       "m": { 
       "type": "object", 
       "properties": { 
        "field": { 
        "type": "object", 
        "properties": { 
         "s": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "s" 
        ] 
        }, 
        "value": { 
        "type": "object", 
        "properties": { 
         "s": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "s" 
        ] 
        }, 
        "operator": { 
        "type": "object", 
        "properties": { 
         "s": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "s" 
        ] 
        } 
       }, 
       "required": [ 
        "field", 
        "value", 
        "operator" 
       ] 
       } 
      } 
      } 
     } 
     }, 
     "required": [ 
     "l" 
     ] 
    }, 
    "calculationId": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    }, 
    "calcDefParameters": { 
     "type": "object", 
     "properties": { 
     "l": { 
      "type": "array", 
      "uniqueItems": true, 
      "minItems": 1, 
      "items": { 
      "required": [], 
      "properties": { 
       "m": { 
       "type": "object", 
       "properties": { 
        "name": { 
        "type": "object", 
        "properties": { 
         "s": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "s" 
        ] 
        }, 
        "value": { 
        "type": "object", 
        "properties": { 
         "n": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "n" 
        ] 
        }, 
        "type": { 
        "type": "object", 
        "properties": { 
         "s": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "s" 
        ] 
        } 
       }, 
       "required": [ 
        "name", 
        "value", 
        "type" 
       ] 
       } 
      } 
      } 
     } 
     }, 
     "required": [ 
     "l" 
     ] 
    }, 
    "runId": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    }, 
    "type": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    }, 
    "externalId": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    }, 
    "dealVersionId": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    } 
    }, 
    "required": [ 
    "definition", 
    "calcDefFilters", 
    "calculationId", 
    "calcDefParameters", 
    "runId", 
    "type", 
    "externalId", 
    "dealVersionId" 
    ] 
} 

Это то, что я получаю в Powershell:

[DBG]: PS C:\Scripts>> $prop 


definition  : @{type=object; properties=; required=System.Object[]} 
calcDefFilters : @{type=object; properties=; required=System.Object[]} 
calculationId  : @{type=object; properties=; required=System.Object[]} 
calcDefParameters : @{type=object; properties=; required=System.Object[]} 
runId    : @{type=object; properties=; required=System.Object[]} 
type    : @{type=object; properties=; required=System.Object[]} 
externalId  : @{type=object; properties=; required=System.Object[]} 
dealVersionId  : @{type=object; properties=; required=System.Object[]} 

Мысли?

+0

Эхх, 'properties' * не * массив, JSON грамматика, которая будет' свойства: [...] 'не 'properties: {...}' –

+0

Право. Таким образом, схема JSON не выполняет свойства в массиве. Тогда мне не повезло? –

+0

[Цитирование через каждый объект NoteProperty в пользовательском объекте] (// stackoverflow.com/q/27642169) – wOxxOm

ответ

0

Не уверен, что он ответит, но вы можете сделать это следующим образом:

$a = @" 
{ 
    "`$schema": "http://json-schema.org/draft-04/schema#", 
    "description": "", 
    "type": "object", 
    "properties": { 
    "definition": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    }, 
    "calcDefFilters": { 
     "type": "object", 
     "properties": { 
     "l": { 
      "type": "array", 
      "uniqueItems": true, 
      "minItems": 1, 
      "items": { 
      "required": [], 
      "properties": { 
       "m": { 
       "type": "object", 
       "properties": { 
        "field": { 
        "type": "object", 
        "properties": { 
         "s": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "s" 
        ] 
        }, 
        "value": { 
        "type": "object", 
        "properties": { 
         "s": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "s" 
        ] 
        }, 
        "operator": { 
        "type": "object", 
        "properties": { 
         "s": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "s" 
        ] 
        } 
       }, 
       "required": [ 
        "field", 
        "value", 
        "operator" 
       ] 
       } 
      } 
      } 
     } 
     }, 
     "required": [ 
     "l" 
     ] 
    }, 
    "calculationId": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    }, 
    "calcDefParameters": { 
     "type": "object", 
     "properties": { 
     "l": { 
      "type": "array", 
      "uniqueItems": true, 
      "minItems": 1, 
      "items": { 
      "required": [], 
      "properties": { 
       "m": { 
       "type": "object", 
       "properties": { 
        "name": { 
        "type": "object", 
        "properties": { 
         "s": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "s" 
        ] 
        }, 
        "value": { 
        "type": "object", 
        "properties": { 
         "n": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "n" 
        ] 
        }, 
        "type": { 
        "type": "object", 
        "properties": { 
         "s": { 
         "type": "string", 
         "minLength": 1 
         } 
        }, 
        "required": [ 
         "s" 
        ] 
        } 
       }, 
       "required": [ 
        "name", 
        "value", 
        "type" 
       ] 
       } 
      } 
      } 
     } 
     }, 
     "required": [ 
     "l" 
     ] 
    }, 
    "runId": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    }, 
    "type": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    }, 
    "externalId": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    }, 
    "dealVersionId": { 
     "type": "object", 
     "properties": { 
     "s": { 
      "type": "string", 
      "minLength": 1 
     } 
     }, 
     "required": [ 
     "s" 
     ] 
    } 
    }, 
    "required": [ 
    "definition", 
    "calcDefFilters", 
    "calculationId", 
    "calcDefParameters", 
    "runId", 
    "type", 
    "externalId", 
    "dealVersionId" 
    ] 
} 
"@ 

$b = $a | ConvertFrom-Json 
$properties = $b.properties.Psobject.Members | where {$_.MemberType -eq 'NoteProperty'} | % {$_.Name} 
+0

Это приводит меня к верхнему уровню, но не к свойствам следующего уровня вниз. –

+0

Вы можете сделать это рекурсивно. – JPBlanc