如何使用jq基于特定值过滤json中的数组

时间:2018-02-23 20:50:54

标签: json bash equality jq

我正在尝试使用jq过滤此json中的数组以仅获取具有“policy_id”的数组:199383 并排除包含不同policy_id值的数组。

{
  "links": {
    "policy_id": 199383,
    "violations": [
      69892478
    ]
  },
  "incident_preference": "PER_CONDITION_AND_TARGET",
  "closed_at": 1519408001909,
  "opened_at": 1519407125437,
  "id": 17821334
}
{
  "links": {
    "policy_id": 199383,
    "violations": [
      69889831
    ]
  },
  "incident_preference": "PER_CONDITION_AND_TARGET",
  "closed_at": 1519408011851,
  "opened_at": 1519406230858,
  "id": 17820349
}
{
  "links": {
    "policy_id": 194774,
    "violations": [
      68446755
    ]
  },
  "incident_preference": "PER_POLICY",
  "closed_at": 1518835775531,
  "opened_at": 1518835745303,
  "id": 17422347
}
{
  "links": {
    "policy_id": 199383,
    "violations": [
      69892488
    ]
  },
  "incident_preference": "PER_CONDITION_AND_TARGET",
  "closed_at": 1519402345676,
  "opened_at": 1519401235467,
  "id": 17821334
}

我试过这个: jq'.incidents [] | select(.links.policy_id ==“199383”)'file.json。但没有任何回报?任何人都可以提供帮助。

谢谢

1 个答案:

答案 0 :(得分:0)

jq '.incidents[]| select(.links.policy_id==199383)' file.json

应该这样做。

<强>输出

{
  "links": {
    "policy_id": 199383,
    "violations": [
      69892478
    ]
  },
  "incident_preference": "PER_CONDITION_AND_TARGET",
  "closed_at": 1519408001909,
  "opened_at": 1519407125437,
  "id": 17821334
}
{
  "links": {
    "policy_id": 199383,
    "violations": [
      69889831
    ]
  },
  "incident_preference": "PER_CONDITION_AND_TARGET",
  "closed_at": 1519408011851,
  "opened_at": 1519406230858,
  "id": 17820349
}
{
  "links": {
    "policy_id": 199383,
    "violations": [
      69892488
    ]
  },
  "incident_preference": "PER_CONDITION_AND_TARGET",
  "closed_at": 1519402345676,
  "opened_at": 1519401235467,
  "id": 17821334
}

来自json.org

  

字符串是包含零个或多个Unicode字符的序列   双引号,使用反斜杠转义。角色表示为   单个字符串。字符串非常类似于C或Java   字符串)。

  

除了八进制之外,数字非常类似于C或Java数字   和十六进制格式使用。

所以199383明显不同于"199383"。它们分别是数字和字符串。

注意:引用文本中的重点是我的。功能