使用JQ

时间:2019-06-09 05:56:43

标签: node.js jq stringify xml2js

我有一个通过XHR提取XML并通过节点模块xlm2js然后通过JSON.stringify进行解析而创建的文件。它有大约700个 segments 两种基本类型。这是文件的编辑版本,每种类型都有一个 segment

{
  "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"]段,并从类型1中提取SegmentIDSegmentNameBeginningDcuIDEndingDcuIDSegmentLength IsActivetrue的细分。

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

  

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

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

1 个答案:

答案 0 :(得分: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"