-1

我通过对 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"
        ]
      }
    ]
  }
}

我需要忽略这些段并从类型 1 段中提取"SegmentType":["2"], SegmentID, SegmentName, BeginningDcuID, EndingDcuIDand 。SegmentLengthIsActivetrue

我可以列出文件,jq "."但任何尝试使用 jq 进行其他操作都会失败,通常会显示以下消息:

'jq:错误:语法错误,意外'['(Unix shell 引用问题?)在,第 1 行:'

任何有关 jq 语法更改或xml2js参数更改的建议都将非常有帮助。

4

1 回答 1

1

如果参数中没有您希望 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"
于 2019-06-09T06:08:42.767 回答