使用 PowerShell 将 JSON 转换为 YAML

使用 PowerShell 将 JSON 转换为 YAML

我有一个像这样的 json 数据。

示例.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"],
  }
]

如何使用 PowerShell 将此 json 数据转换为 yaml其中每个 json 对象将转换为 yaml 并以 yaml 格式保存在其自己的文件中,文件名为 title 键属性的值

当我运行以下命令时($json | ConvertFrom-Json) | ConvertTo-YAML(其中ConvertTo-YAML函数取自simpletalk 网站),这是我得到的输出。

输出

---

 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'

但是,我正在寻找的输出看起来像这样 - 其中文件名是标题键属性的值,文件的内容是转换为 yaml 的相应 json 对象。

最低限度的劳动是无用的.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'
---

脂肪肝可以导致哪些疾病?

---
 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'
---

非最低限度的动物不具有对精英的有效性.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'
---

答案1

我正在回答我自己的问题以防其他人正在寻找答案。

$obj = ($json  | ConvertFrom-Json)

ForEach($item in $obj) {
    $filename = "$($item.title).yaml"
    $item | ConvertTo-YAML > $filename
    "---" >> $filename
}

相关内容