【问题标题】:jq return a json array in a very specifique wayjq 以一种非常具体的方式返回一个 json 数组
【发布时间】:2021-06-08 14:21:24
【问题描述】:

我有这个Json(是一个测试数据库,这里没有数据是真的)

{
  "pguid": "4EA979A2-E578-4DA3-89DB-24082F3092AA",
  "lastEnrollTguid": "EA98B161-04D3-4F0A-920A-58DBFF3C2274",
  "timestamp": 1016086888000,
  "keys": [
    {
      "id": "gr",
      "value": "1907971"
    }
  ],
  "biographics": [
    {
      "id": "localNascimento",
      "value": "JOINVILLE SC"
    },
    {
      "id": "dataNascimento",
      "value": "1859-03-08"
    },
    {
      "id": "mae",
      "value": "ANTA MARCIA PINHEAD"
    },
    {
      "id": "nome",
      "value": "MIR PINHEAD"
    }
  ],
  "biometric": [
    {
      "source": "ORIGINAL",
      "type": "FACE",
      "format": "JPEG",
      "properties": {
        "width": 0,
        "height": 0,
        "resolution": 500,
        "ratio": 0,
        "matcherId": 0,
        "extractorId": 0
      },
      "index": 10,
      "content": "5215421547"
    }
  ],
  "labels": [
    "SC",
    "CIVIL",
    "MASCULINO",
    "JOINVILLE"
  ],
  "history": {
    "events": [
      {
        "type": "ENROLL",
        "tguid": "3C1B0D1F-9143-4C24-A351-E88A19317AC9",
        "timestamp": 1014086658288
      },
      {
        "type": "UPDATE",
        "tguid": "EA98B161-04D3-4F0A-920A-58DBFF3C2274",
        "timestamp": 1016786888028
      }
    ]
  }
}

我只想检索历史数组中的 de tguid,如果存在这样做的方法,请使用数组的 de 索引来完成。

在这里,我试图实现这一点(不幸失败了) 示例(它不起作用):

jq  '.[].history.events.tguid[1]' /tmp/teste.json

我想检索索引中的 pguid 以使用它。 有人有什么想法吗?

【问题讨论】:

  • tguid 显然不是一个数组。
  • 如果你想创建一个名为tguid的数组:jq '{ "tguid": [.history.events[].tguid] }' /tmp/teste.json。然后您可以将此数组寻址为.tguid[1],如下所示:jq '{ "tguid": [.history.events[].tguid] } | .tguid[1]'
  • 根据minimal reproducible example 准则,请显示所需的输出。

标签: json linux bash jq


【解决方案1】:

试试这个

jq  '.history.events | .[1].tguid' /tmp/teste.json

【讨论】:

  • tnks 为您的帖子。我试过了,我收到了“null”#jq '.history.events | .[1].tguid' /tmp/teste.json #null
【解决方案2】:

大家都知道

jq  '.[].history.events | .[0].tguid' /tmp/teste1.json

【讨论】:

  • 致其他答案审阅者:这实际上与其他答案和 cmets 不同,因此实际上应该保留在此站点上的该问题的答案。
  • @Dominique 我需要等待 20 小时才能接受我自己的答案。
猜你喜欢
  • 1970-01-01
  • 2023-02-18
  • 2019-04-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多