考虑这两个JSON文件:
{
"tables": [
{
"columns": [
{
"name": "action",
"type": "string"
},
{
"name": "region",
"type": "string"
},
{
"name": "count_",
"type": "long"
},
{
"name": "min_timestamp",
"type": "datetime"
},
{
"name": "max_timestamp",
"type": "datetime"
}
],
"name": "PrimaryResult",
"rows": [
[
"ChangePassword",
"CN",
1,
"2022-07-19T11:51:52.8430729Z",
"2022-07-19T11:51:52.8430729Z"
]
]
}
]
}
{
"tables": [
{
"columns": [
{
"name": "action",
"type": "string"
},
{
"name": "region",
"type": "string"
},
{
"name": "count_",
"type": "long"
},
{
"name": "min_timestamp",
"type": "datetime"
},
{
"name": "max_timestamp",
"type": "datetime"
}
],
"name": "PrimaryResult",
"rows": [
[
"ChangePassword",
"CN",
1,
"2022-07-19T11:51:52.8430729Z",
"2022-07-19T11:51:52.8430729Z"
],
[
"Register",
"CN",
1,
"2022-07-19T11:51:52.8430729Z",
"2022-07-19T11:51:52.8430729Z"
]
]
}
]
}
它们具有完全相同的模式,并且几乎相同.不同之处在于,第一个文件在rows
属性中有1行,第二个文件在rows
属性中有2行.
我使用ConvertFrom-Json
将文件加载到PowerShell.当我开始访问.tables.rows[0]
时,我希望它将返回第一行.
然而,在只有一行的第一个文件中,它实际上返回第一行的第一列.
(gc file1.json | ConvertFrom-Json).tables.rows[0]
输出:
ChangePassword
如果第二个文件中有超过1行,则.tables.rows[0]
的行为与预期相同.
(gc file2.json | ConvertFrom-Json).tables.rows[0]
输出:
ChangePassword
CN
1
2022-07-19T11:51:52.8430729Z
2022-07-19T11:51:52.8430729Z
我如何在PowerShell中可靠地处理这些JSON文件,而不管其中是一行还是多行?
顺便说一句,这些实际上是az cli
生成的JSON文件,是Azure Application Insights查询的结果.