Ich beantworte meine eigene Frage, falls jemand anderes nach einer Antwort sucht.
$obj = ($json | ConvertFrom-Json) ForEach($item in $obj) { $filename = "$($item.title).yaml" $item | ConvertTo-YAML > $filename "---" >> $filename }
Ich habe solche Json-Daten.
sample.json
[ { "id": 0, "name": "Cofine", "title": "laboris minim qui nisi esse amet non", "description": "Consequat laborum quis exercitation culpa. Culpa esse sint consectetur deserunt non.", "website": "cofine.com", "image": "http://placehold.it/32x32", "labels": ["blue", "red"], "labels_link": ["http://cofine.com/labels/blue","http://cofine.com/labels/red"], }, { "id": 1, "name": "Zomboid", "title": "adipisicing mollit esse aliquip ullamco nisi laboris", "description": "Enim consectetur eu commodo officia. Id pariatur proident nostrud occaecat adipisicing voluptate do nisi incididunt id ex commodo.", "website": "zomboid.com", "image": "http://placehold.it/32x32", "labels": ["red"], "labels_link": ["http://zomboid.com/labels/red"], }, { "id": 2, "name": "Sulfax", "title": "non minim anim irure nulla ad elit", "description": "Pariatur anim officia adipisicing Lorem dolor cillum eu ex veniam sint consequat incididunt. Minim mollit reprehenderit mollit sint laboris consequat.", "website": "sulfax.com", "image": "http://placehold.it/32x32", "labels": ["green", "yellow", "blue"], "labels_link": ["http://sulfax.com/labels/green","http://sulfax.com/labels/yellow","http://sulfax.com/labels/blue"], } ]
Wie konvertiere ich diese Json-Daten mithilfe von PowerShell in yaml, wobei jedes Json-Objekt in yaml konvertiert und in seiner eigenen Datei als yaml gespeichert wird, wobei der Dateiname der Wert der Eigenschaften der Titelschlüssel ist ?
Wenn ich den folgenden Befehl ausführe ($json | ConvertFrom-Json) | ConvertTo-YAML
(wobei die ConvertTo-YAML
Funktion von der simpletalk-Website übernommen wird ), ist dies die Ausgabe, die ich bekomme.
Ausgabe
--- id: 0 name: 'Cofine' title: 'laboris minim qui nisi esse amet non' description: Consequat laborum quis exercitation culpa. Culpa esse sint consectetur deserunt non. website: 'cofine.com' image: 'http://placehold.it/32x32' labels: - 'blue' - 'red' labels_link: - 'http://cofine.com/labels/blue' - 'http://cofine.com/labels/red' --- id: 1 name: 'Zomboid' title: 'adipisicing mollit esse aliquip ullamco nisi laboris' description: Enim consectetur eu commodo officia. Id pariatur proident nostrud occaecat adipisicing voluptate do nisi incididunt id ex commodo. website: 'zomboid.com' image: 'http://placehold.it/32x32' labels: - 'red' labels_link: - 'http://zomboid.com/labels/red' --- id: 2 name: 'Sulfax' title: 'non minim anim irure nulla ad elit' description: Pariatur anim officia adipisicing Lorem dolor cillum eu ex veniam sint consequat incididunt. Minim mollit reprehenderit mollit sint laboris consequat. website: 'sulfax.com' image: 'http://placehold.it/32x32' labels: - 'green' - 'yellow' - 'blue' labels_link: - 'http://sulfax.com/labels/green' - 'http://sulfax.com/labels/yellow' - 'http://sulfax.com/labels/blue'
Die Ausgabe, nach der ich suche, würde jedoch so aussehen - wobei der Dateiname der Wert der Eigenschaften der Titelschlüssel ist und der Inhalt der Datei das entsprechende Json-Objekt sein soll, das in Yaml konvertiert wird.
minimis qui nisi esse amet non.yaml
--- id: 0 name: 'Cofine' title: 'laboris minim qui nisi esse amet non' description: Consequat laborum quis exercitation culpa. Culpa esse sint consectetur deserunt non. website: 'cofine.com' image: 'http://placehold.it/32x32' labels: - 'blue' - 'red' labels_link: - 'http://cofine.com/labels/blue' - 'http://cofine.com/labels/red' ---
Mollit esse aliquip ullamco nisi laboris.yaml
--- id: 1 name: 'Zomboid' title: 'adipisicing mollit esse aliquip ullamco nisi laboris' description: Enim consectetur eu commodo officia. Id pariatur proident nostrud occaecat adipisicing voluptate do nisi incididunt id ex commodo. website: 'zomboid.com' image: 'http://placehold.it/32x32' labels: - 'red' labels_link: - 'http://zomboid.com/labels/red' ---
non minim anim irure nulla ad elit.yaml
--- id: 2 name: 'Sulfax' title: 'non minim anim irure nulla ad elit' description: Pariatur anim officia adipisicing Lorem dolor cillum eu ex veniam sint consequat incididunt. Minim mollit reprehenderit mollit sint laboris consequat. website: 'sulfax.com' image: 'http://placehold.it/32x32' labels: - 'green' - 'yellow' - 'blue' labels_link: - 'http://sulfax.com/labels/green' - 'http://sulfax.com/labels/yellow' - 'http://sulfax.com/labels/blue' ---
Ich beantworte meine eigene Frage, falls jemand anderes nach einer Antwort sucht.
$obj = ($json | ConvertFrom-Json) ForEach($item in $obj) { $filename = "$($item.title).yaml" $item | ConvertTo-YAML > $filename "---" >> $filename }