[英]Traverse JSON Schema in Powershell
我正在嘗試從JSON數據進行一些代碼生成。 我正在將一些AWS備份數據放入JSON文件中。 我獲取第一條記錄,並從那一行生成一個json模式。
在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[]}
思考?
不確定它的答案,但是您可以這樣做:
$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}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.