我有一个像这样的json数据。
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"],
}
]
如何使用PowerShell将此json数据转换为yaml,其中每个json对象都将转换为yaml并另存为yaml在其自己的文件中,且文件名是标题键属性的值?
当我运行以下命令($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对象。
最低劳动生产率
---
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'
---
非最低限度的动画无效广告
---
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'
---
你读过这个吗?simple-talk.com/sysadmin/powershell/…–
—
SadBunny
@SadBunny是的,我有
—
Ishan
似乎已针对您的确切目的进行了完整的实现。没有?
—
SadBunny
@SadBunny,不,那篇文章并不能完全满足我的要求。看到我更新的问题。
—
伊桑
好吧,那肯定是一个非常特定的功能...那么,您想将传入的数据拆分为一个或多个文件,文件名取决于特定键的值吗?那将意味着您必须调整当前代码来做到这一点。如果您不知道如何,最好将代码发布到堆栈溢出并寻求帮助以将代码更改为您的功能。可能并不难,您必须拆分YAML对象,遍历它们,获取值,然后保存该对象。可能的捷径是在总结果中进行大量的正则表达式搜索和替换。
—
SadBunny