【问题标题】:Parse result of XML -> XML2JS ->JSON.stringify using JQ使用 JQ 解析 XML -> XML2JS ->JSON.stringify 的结果
【发布时间】:2019-10-24 00:34:46
【问题描述】:

我有一个通过 XHR 提取 XML 并通过节点模块 xlm2jsJSON.stringify 解析它创建的文件。它有两种基本类型的大约 700 个。这是文件的编辑版本,每种类型都有一个 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"] 段并从IsActivetrue 的类型1 段中提取SegmentIDSegmentNameBeginningDcuIDEndingDcuIDSegmentLength

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

'jq: error: syntax error, unexpected '[' (Unix shell quoting issues?) at , line 1:'

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

【问题讨论】:

    标签: node.js jq stringify xml2js


    【解决方案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"
    

    【讨论】:

    • 谢谢,我没有在帖子中添加返回,因为我不确定它们是否会影响 JQ 解析。显然不是。
    猜你喜欢
    • 2019-03-15
    • 2014-08-30
    • 2012-06-09
    • 2018-02-23
    • 1970-01-01
    • 1970-01-01
    • 2018-10-31
    • 2017-09-04
    • 2018-01-02
    相关资源
    最近更新 更多