json 沿着数组复制单个值的JOLT转换

sqxo8psd  于 2023-03-13  发布在  其他
关注(0)|答案(2)|浏览(98)

我希望将JSON转换为键-值对,并同时将特定的单个值(即"timestamp")复制到所有这些对中。
输入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规范,它已经为所有不是“Timestamp”的条目生成了键-值对,但是我如何将“Timestamp”的值复制到这些记录中的每一条中呢?

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

上述JOLT规范的输出:

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

ijnw1ujt1#

质量标准

[
  {
    // 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"
        }
      }
    }
  }
]
ddrv8njm

ddrv8njm2#

您可以在移位转换规范中循环以**_key**结尾的属性,例如

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

其中**"@1,Timestamp"表示遍历树的一个级别后获取Timestamp属性的值
http://jolt-demo.appspot.com/站点上的
演示**是

相关问题