使用Jolt从第一个数组元素中提取值

时间:2017-01-06 16:55:14

标签: json jolt

我有以下JSON文档:

{
    "pbid": 123,
    "pid": 0,
    "time": 1483551745000,
    "timestamp": "2017-01-04 17:42:25",
    "creationTime": 1483551789000,
    "creationTimestamp": "2017-01-04 17:43:09",
    "name": "myname",
    "triggeredComponents": [
        {
            "device": {
                "did": 20,
                "ip": "127.0.0.1",
                "firstSeen": 1427474095000,
                "lastSeen": 1483545006000,
                "typename": "dnsserver"
            },
            "time": 1483551789000
        }
    ]
}

使用Jolt我需要将其转换为以下内容:

{
  "event_id" : 123,                ( pbid )
  "name" : "myname",               ( name )
  "did": "20",                     ( triggeredComponents[0].device.did )
  "first_seen": 1427474095000,     ( triggeredComponents[0].device.firstSeen )
  "last_seen": 1483545006000       ( triggeredComponents[0].device.lastSeen )
}

我对基本班次(event_idname)很好,但我无法弄清楚如何从数组中提取。这是我目前的尝试(我也尝试过其他一些方法):

[
  {
    "operation": "shift",
    "spec": {
      "pbid": "event_id",
      "name": "name",
      "triggeredComponents" : {
        "*": {
          "did": "triggeredComponents[&1].device.did",
          "first_seen": "triggeredComponents[&1].device.firstSeen",
          "last_seen": "triggeredComponents[&1].device.lastSeen"
        }
      }
    }
  }
]

在与供应商追逐后,似乎triggeredComponents数组只包含一个对象,所以我只需要查看第0个元素。

1 个答案:

答案 0 :(得分:3)

规格 [ { "operation": "shift", "spec": { "pbid": "event_id", "name": "name", "triggeredComponents": { "0": { "device": { "did": "did", "firstSeen": "first_seen", "lastSeen": "last_seen" } } } } } ]

你错过了"设备"介于" triggeredComponents [0]"和"做了#34;。