2017-02-07 56 views
0

我正在尝试从JSON数据做一些代码生成。我将一些AWS备份数据导入JSON文件。我抓住第一条记录并从那一行生成一个json模式。在PowerShell中遍历JSON Schema

当我在Powershell中加载模式时,我无法枚举属性,因为它们被反序列化为主属性,而不是作为“属性”中的数组。这看起来像一个JSON模式的东西,其中的属性列表显示为一个对象列表而不是一个数组,但我不知道。

我猜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

EHH,'properties'是*不*数组,一个JSON语法将'属性:[...]'不'属性:{...}' –

+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