json - PowerShell ConvertFrom-Json 未将 JSON 列表转换为 Json 对象
问题描述
我有一个 json 输出作为列表,我想使用 PowerShell 在 tags 参数中添加一个属性
$json = @'
[
{
"tags": [],
"name": "[concat(parameters('factoryName'), '/Veh_Obj')]",
"properties": {
"type": "AzureDataLakeStoreFile",
}
}
]
'@
#$json = 'C:\workspace\cucumber_report.29776.json'
$obj = $json | ConvertFrom-Json
# Append new objects to the array.
$obj.tags += [pscustomobject] @{ name = 'newname1' }
# Convert back to JSON.
$obj | ConvertTo-Json -Depth 10 | Set-Content $json
但我收到如下错误 在此对象上找不到属性“标签”。验证该属性是否存在并且可以设置。 看起来只有当我传递 JSON 列表时才会出现问题,使用正常的 json 输入,这工作正常
解决方案
- 行中的尾随逗号
"type": "AzureDataLakeStoreFile",
导致错误ConvertFrom-Json : Invalid JSON primitive
。 $obj.GetType().Name
返回Object[]
(注意原始的最顶层元素json
是一个数组[]
)。
利用
$obj[0].tags += [pscustomobject] @{ name = 'newname1' }
推荐阅读
- python - Python:json.load 无法从文件中读取数据
- python - 如果列中存在重合,则根据另一个 df 中的值替换 df 中的值
- powershell - Powershell 无法完全匹配文件夹全名
- angular - 如何为角度组件创建动态结构?
- excel - 关闭工作簿时出现内存不足错误 - Excel VBA
- reactjs - 如何覆盖 material-ui 变量或函数
- jsonschema - JSONSchema 外键检查
- typescript - 嵌套对象内的返回类型联合
- mongodb - MongoDB——存储策略
- sql - sql中是否存在将一个表的缺失数据填充到另一个表的连接?