arrays - 来自 Azure DevOps Api 的 Powershell Json 数组内容
问题描述
{
"count": 3,
"value": [
{
"id": "12345678-123456-23424-123456ff2",
"name": "TestProject",
"description": "Test project migration",
"url": "https://dev.azure.com",
"state": "wellFormed",
"revision": 6619,
"visibility": "private",
"lastUpdateTime": "2019-10-14T06:10:03.557Z"
},
{
"id": "12345678-123456-23424-123456ff2",
"name": "KC-TestAutomation-Framework",
"description": "Test Automation Frameworks",
"url": "https://dev.azure.com",
"state": "wellFormed",
"revision": 6502,
"visibility": "private",
"lastUpdateTime": "2019-10-03T07:53:33.95Z"
},
{
"id": "b2345678-123456-23424-12345",
"name": "Training",
"description": "Training Management Project",
"url": "https://dev.azure.com",
"state": "wellFormed",
"revision": 7124,
"visibility": "private",
"lastUpdateTime": "2019-12-02T07:19:24Z"
}
]
}
我有这个 Json,我需要从这个 json 创建另一个 json,其中包含每个项目的名称和 ID,格式为 name:name value:id in json file...
我试过这样的东西..
$json = Get-Content $file | ConvertFrom-Json
ForEach($i in $json.value.id)
{
Write-Host "Id: $($i)","name: $($json.value.name)"
}
解决方案
- 读取 JSON 文件并使用
ConvertFrom-Json
. - 选择 id 和 name 属性并转换回 json
- 将新创建的 json 写入文件
像这样的东西:
$json = Get-Content -Path 'X:\YourJsonFile.json' | ConvertFrom-Json
$newJson = $json.value | Select-Object id, name | ConvertTo-Json
# output on screen
$newJson
# output to new json file
$newJson | Set-Content -Path 'X:\YourNewJsonFile.json'
输出:
[ { "id": "12345678-123456-23424-123456ff2", "name": "TestProject" }, { "id": "12345678-123456-23424-123456ff2", "name": "KC-TestAutomation-Framework" }, { "id": "b2345678-123456-23424-12345", "name": "Training" } ]
推荐阅读
- python - 如何检测质心并测量两个物体之间的距离?使用鸟瞰?
- mqtt - Mosquitto_sub 错误:连接被拒绝,仅限 IPv6
- python - Android自动化测试:无法在文本字段中输入文本
- unity3d - 眼睛光标固定在 FOV 中心,HoloLens 上没有手/手势识别
- intellij-idea - 无法使用 Glassfish 服务器 5 在 Intellij Idea 中运行 JSF/Primefaces 项目
- python - Python PYJQ 将嵌套字段映射到多个顶级字段
- sql-server - 仅将特定 XML 参数导入 SQL Server
- firebase - Firebase Web 推送通知 (FCM):潜在用户与发送
- maven - 自定义 Azure DevOps 任务已满足部分但未满足需求
- bash - 试图在字符串模式中找到 2 次或更多次出现