【发布时间】:2017-09-06 19:03:42
【问题描述】:
我正在尝试解析一个 JSON 字符串,该字符串从一个关系数据集的外观看已被扁平化...转换为 CSV,以便导入 Oracle 数据库。我有类似于这个(简化)示例的名称/值对
Device.1.Service.1.Channel.1.someProperty : 1,
Device.1.Service.1.Channel.1.someOtherProperty : "billy",
Device.1.Service.1.Channel.2.someProperty : 8,
Device.1.Service.1.Channel.2.someOtherProperty : "frank",
Device.1.Service.1.Channel.3.someProperty : 12,
Device.1.Service.1.Channel.3.someOtherProperty : "sam",
Device.1.Service.2.Channel.1.someProperty : 3,
Device.1.Service.2.Channel.1.someOtherProperty : "john",
编辑:每个设备每天都会生成一个具有类似结构的 .json 文件。因此,当合并 (Get-Content .\*.json -Raw) 时,我会在控制台中看到相同的属性名称多次出现。
作为转换的一部分,我希望部分属性名称成为数据库中的字段。这将使我们能够在以后使用动态滑块过滤器等更好地可视化数据。
|设备 |服务 |频道 |一些其他属性 | | 1 | 1 | 1 |比利 | | 1 | 1 | 2 |坦率 | | 1 | 1 | 3 |山姆 |现在,我正在使用 cmdlet ConvertFrom-Json。然后,我使用通配符选择字段(从近 2000 个可能的字段中)。例如,频道的数量是动态的。
Get-Content .\*.json -Raw | ConvertFrom-Json |
Select Device.1.Service.1.Channel.?.someOtherProperty
返回PSCustomObject。我想弄清楚频道号并将其用作派生字段。伪例子:
Select @{n="Channel";e={$_.getCharAtWildcard()}},
$_.theValueofTheCurrentObject()
如果我将选定的数据(使用通配符)通过管道传输到 Get-Member,PSCostomObject 包含方法名称以及我的字段名称(但不包含值)。
我对如何访问字段名称感到困惑(因为“名称”给出了空白行),然后提取通道号(通配符位置的字符),然后是正确构造我的输出的值。
有什么建议吗?谷歌搜索并有一个 lynda.com 子,但似乎无法找到解决这个特定问题的方法 - 可能是因为我没有使用正确的术语?
## ANSGAR's SOLUTION - WORKS FOR SINGLE FILE ##
$dataDir = "C:\ps_json2csv\dummydata"
CD $dataDir
$dict = Get-Content .\*.json -Raw | ConvertFrom-Json | Select -Expand data
$p = 'DvbId'
$re = "frontend\.(\d+)\.logicalchannel\.(\d+)\.service\.(\d+)\..*?\.$p"
## modified regex to match my data, example string: FrontEnd.2.LogicalChannel.3.Service.1.stat.DvbId
$fullset = $dict.PSObject.Properties | Where-Object {
$_.Name -match $re
} | ForEach-Object {
$prop = [ordered]@{
FrontEnd = $matches[1]
LogicalChannel = $matches[2]
Service = $matches[3]
$p = $_.Value
}
New-Object -Type PSObject -Property $prop
}
## inspect $dict - its populated
## inspect $fullset - its empty! :(
其中的数据是 C:\ps_json2csv\dummydata 中包含的 2 个文件:
文件1.json
{
"data": {
"Device.1.Service.1.ChannelInfo.Channel.1.Stats.someProperty" : "1",
"Device.1.Service.1.ChannelInfo.Channel.2.Stats.someProperty" : "8",
"Device.1.Service.1.ChannelInfo.Channel.3.Stats.someProperty" : "12",
"FrontEnd.2.LogicalChannel.3.Service.1.stat.DvbId" : "john",
"FrontEnd.2.LogicalChannel.3.Service.2.stat.DvbId" : "billy",
"FrontEnd.2.LogicalChannel.3.Service.3.stat.DvbId" : "frank",
"FrontEnd.2.LogicalChannel.4.Service.1.stat.DvbId" : "sam",
"Device.1.Service.2.ChannelInfo.Channel.1.Stats.someProperty" : "3",
"Some.value.im.not.intersted.in.just.yet": "Sat Jan 1 00:00:00 GMT 0001",
"foo.bar" : "0",
"random.stuff" : "hi there"
}
}
文件2.json
{
"data": {
"Device.1.Service.1.ChannelInfo.Channel.1.Stats.someProperty" : "0",
"Device.1.Service.1.ChannelInfo.Channel.2.Stats.someProperty" : "7",
"Device.1.Service.1.ChannelInfo.Channel.3.Stats.someProperty" : "6",
"FrontEnd.2.LogicalChannel.3.Service.1.stat.DvbId" : "john",
"FrontEnd.2.LogicalChannel.3.Service.2.stat.DvbId" : "billy",
"FrontEnd.2.LogicalChannel.3.Service.3.stat.DvbId" : "frank",
"FrontEnd.2.LogicalChannel.4.Service.1.stat.DvbId" : "sam",
"Device.1.Service.2.ChannelInfo.Channel.1.Stats.someProperty" : "4",
"Some.value.im.not.intersted.in.just.yet": "Sun Jan 2 00:00:00 GMT 0001",
"foo.bar" : "0",
"random.stuff" : "hi there"
}
}
【问题讨论】:
标签: json powershell