2017-02-07 9 views
0

Ich versuche, einige Code-Generierung aus JSON-Daten zu tun. Ich erhalte einige AWS-Sicherungsdaten in einer JSON-Datei. Ich nehme den ersten Datensatz und erzeuge ein JSON-Schema aus dieser einen Zeile.Traverse JSON-Schema in Powershell

Wenn ich das Schema in Powershell lade, kann ich die Eigenschaften nicht aufzählen, weil sie als primäre Eigenschaften deserialzed werden, nicht als ein Array innerhalb von "Eigenschaften". Das sieht wie ein JSON-Schema aus, bei dem die Liste der Eigenschaften als Objektliste und nicht als Array angezeigt wird, aber ich bin mir nicht sicher.

Ich vermute, der PowerShell JSON-Parser kann dort kein Array sehen, also macht sie nur Eigenschaften.

Ich möchte wirklich nicht das Schema JSON manuell durchlaufen, um die Liste der Eigenschaften und ihrer Typen zu erhalten.

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" 
    ] 
} 

Dies ist, was ich in Powershell erhalten:

[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[]} 

Gedanken?

+0

Ehh, 'properties' ist * nicht * ein Array, json Grammatik für das wäre' Eigenschaften werden: [...] 'nicht 'Eigenschaften: {...}' –

+0

Richtig. JSON-Schemas machen also keine Eigenschaften in einem Array. Ich bin dann kein Glück mehr? –

+0

[Looping durch jede NoteProperty in einem benutzerdefinierten Objekt] (// stackoverflow.com/q/27642169) – wOxxOm

Antwort

0

nicht sicher, dass es beantworten, aber Sie können es tun, wie folgt:

$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

Das bringt mich auf die oberste Ebene, aber nicht die nächste Ebene nach unten Eigenschaften. –

+0

Sie können es rekursiv tun. – JPBlanc

Verwandte Themen