【问题标题】:JOLT transformation to copy single value along an arrayJOLT 转换以沿数组复制单个值
【发布时间】:2018-05-18 14:10:41
【问题描述】:

我想将 JSON 转换为键值对,同时将特定的单个值(即“时间戳”)复制到所有这些对。

输入 JSON:

{
  "Timestamp": "2018-05-13T14:57:09",
  "first_key": "1023",
  "another_key": "1987",
  "yet_another_key": "677"
}

预期输出:

[ {
    "Timestamp": "2018-05-13T14:57:09.087",
    "key": "first_key",
    "value": "1023"
  },
  {
    "Timestamp": "2018-05-13T14:57:09.087",
    "key": "another_key",
    "value": "1987"
  },
  {
    "Timestamp": "2018-05-13T14:57:09.087",
    "key": "yet_another_key",
    "value": "677"
  }]

到目前为止,我想出的是以下 JOLT 规范。它已经为所有不是“时间戳”的条目生成了键值对,但是如何将“时间戳”的值复制到每个记录中?

[{
  "operation": "shift",
  "spec": {
    "Timestamp": "[].Timestamp",
    "*": {
      "$": "[#2].key",
      "@": "[#2].value"
    }
  }
}]

上述 JOLT 规范的输出:

[ {
  "Timestamp" : "2018-05-13T14:57:09"
}, {
  "key" : "first_key",
  "value" : "1023"
}, {
  "key" : "another_key",
  "value" : "1987"
}, {
  "key" : "yet_another_key",
  "value" : "677"
} ]

【问题讨论】:

    标签: json jolt


    【解决方案1】:

    规格

    [
      {
        // first separate the Timestamp from the fields that 
        //  are going to be pivoted.
        // This is needed because the "[#2]" logic 
        //  isn't doing what you think it is / 
        //  you would end up with a null in your output array.
        // Basically the "[#2]" logic as written only works
        //  when you are cleanly pivoting data.  Which you are
        //  not because "Timestamp" and "first_key" are siblings.
        "operation": "shift",
        "spec": {
          "Timestamp": "Timestamp",
          "*": "keysToPivot.&"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "keysToPivot": {
            "*": {
              // Now that we can cleanly loop thru _only_ the 
              //  keysToPivot, the [#2] logic will work "right".
              // Additionally, lookup the Timestamp value
              //  and add it to each "pivoted" output.
              "$": "[#2].key",
              "@": "[#2].value",
              "@(2,Timestamp)": "[#2].Timestamp"
            }
          }
        }
      }
    ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-06
      • 2021-10-29
      • 1970-01-01
      相关资源
      最近更新 更多