使用 JQ 解析 XML -> XML2JS ->JSON.stringify 的结果
Parse result of XML -> XML2JS ->JSON.stringify using JQ
我有一个文件是通过执行 XML 的 XHR 提取并通过节点模块 xlm2js
然后 JSON.stringify
解析它而创建的。它有大约 700 个 段 两种基本类型。这是文件的编辑版本,每种类型有一个 段 :
{
"NewDataSet": {
"Table": [
{
"SegmentID": [
"2342"
],
"StationID": [
"005es00045:_MN_Stn"
],
"SegmentName": [
"I-5 NB MP0.45 @ SR-14"
],
"SegmentType": [
"2"
],
"SegmentLength": [
"1135"
],
"MinimumLanesReporting": [
"0.5"
],
"CalculationThreshold": [
"30"
],
"CalculationPeriod": [
"2"
],
"MinimumSamples": [
"3"
],
"SegmentMaximumFilter": [
"774"
],
"SegmentMinimumFilter": [
"12"
],
"StandardDeviationSamples": [
"15"
],
"StandardDeviationMultiplier": [
"1.96"
],
"UseStandardDeviationFilter": [
"false"
],
"IsActive": [
"true"
]
},
{
"SegmentID": [
"3051"
],
"BeginningDcuID": [
"584"
],
"EndDcuID": [
"589"
],
"SourceSystem": [
"TravelTime"
],
"SegmentName": [
"OR212 at SE 242nd Ave to OR212 at SE Foster Rd"
],
"SegmentType": [
"1"
],
"SegmentLength": [
"100"
],
"CalculationThreshold": [
"60"
],
"CalculationPeriod": [
"10"
],
"MinimumSamples": [
"3"
],
"SegmentMaximumFilter": [
"3600"
],
"SegmentMinimumFilter": [
"50"
],
"StandardDeviationSamples": [
"20"
],
"StandardDeviationMultiplier": [
"1.96"
],
"UseStandardDeviationFilter": [
"true"
],
"IsActive": [
"true"
]
}
]
}
}
我需要忽略 "SegmentType":["2"]
段并从中提取 SegmentID
、SegmentName
、BeginningDcuID
、EndingDcuID
和 SegmentLength
输入 1 段,其中 IsActive
是 true
.
我可以使用 jq "."
列出文件,但是使用 jq 进行其他操作的任何尝试都会失败,通常会显示以下消息:
'jq: error: syntax error, unexpected '[' (Unix shell quoting issues?) at , line 1:'
任何有关 jq 语法更改或 xml2js
参数更改的建议都将非常有帮助。
如果您希望 shell 展开任何内容,请不要使用双引号来引用参数。
$ jq '.NewDataSet.Table[]
| select(.SegmentType[0] != "2" and .IsActive[0] == "true")
| (.SegmentID, .SegmentName, .BeginningDcuID, .EndingDcuID, .SegmentLength)[0]' file
"3051"
"OR212 at SE 242nd Ave to OR212 at SE Foster Rd"
"584"
null
"100"
我有一个文件是通过执行 XML 的 XHR 提取并通过节点模块 xlm2js
然后 JSON.stringify
解析它而创建的。它有大约 700 个 段 两种基本类型。这是文件的编辑版本,每种类型有一个 段 :
{
"NewDataSet": {
"Table": [
{
"SegmentID": [
"2342"
],
"StationID": [
"005es00045:_MN_Stn"
],
"SegmentName": [
"I-5 NB MP0.45 @ SR-14"
],
"SegmentType": [
"2"
],
"SegmentLength": [
"1135"
],
"MinimumLanesReporting": [
"0.5"
],
"CalculationThreshold": [
"30"
],
"CalculationPeriod": [
"2"
],
"MinimumSamples": [
"3"
],
"SegmentMaximumFilter": [
"774"
],
"SegmentMinimumFilter": [
"12"
],
"StandardDeviationSamples": [
"15"
],
"StandardDeviationMultiplier": [
"1.96"
],
"UseStandardDeviationFilter": [
"false"
],
"IsActive": [
"true"
]
},
{
"SegmentID": [
"3051"
],
"BeginningDcuID": [
"584"
],
"EndDcuID": [
"589"
],
"SourceSystem": [
"TravelTime"
],
"SegmentName": [
"OR212 at SE 242nd Ave to OR212 at SE Foster Rd"
],
"SegmentType": [
"1"
],
"SegmentLength": [
"100"
],
"CalculationThreshold": [
"60"
],
"CalculationPeriod": [
"10"
],
"MinimumSamples": [
"3"
],
"SegmentMaximumFilter": [
"3600"
],
"SegmentMinimumFilter": [
"50"
],
"StandardDeviationSamples": [
"20"
],
"StandardDeviationMultiplier": [
"1.96"
],
"UseStandardDeviationFilter": [
"true"
],
"IsActive": [
"true"
]
}
]
}
}
我需要忽略 "SegmentType":["2"]
段并从中提取 SegmentID
、SegmentName
、BeginningDcuID
、EndingDcuID
和 SegmentLength
输入 1 段,其中 IsActive
是 true
.
我可以使用 jq "."
列出文件,但是使用 jq 进行其他操作的任何尝试都会失败,通常会显示以下消息:
'jq: error: syntax error, unexpected '[' (Unix shell quoting issues?) at , line 1:'
任何有关 jq 语法更改或 xml2js
参数更改的建议都将非常有帮助。
如果您希望 shell 展开任何内容,请不要使用双引号来引用参数。
$ jq '.NewDataSet.Table[]
| select(.SegmentType[0] != "2" and .IsActive[0] == "true")
| (.SegmentID, .SegmentName, .BeginningDcuID, .EndingDcuID, .SegmentLength)[0]' file
"3051"
"OR212 at SE 242nd Ave to OR212 at SE Foster Rd"
"584"
null
"100"