bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.54k stars 329 forks source link

How to transform the single json to json arrays? #1107

Open 953077065 opened 2 years ago

953077065 commented 2 years ago

Hi~ I'd like to transform json formart and calculate the data_time , how can I do this ? here is the source and the target json text: Source:

{
  "ID": "123456789",
  "METER_ID": "987654312",
  "DATA_DATE": "2021-11-01 00:00:00",
  "FLAG": "2",
  "I1": 1,
  "I2": 2,
  "I3": 3,
  "I4": 4,
  "I5": 5
}

Target:

[
  {
    "ID": "123456789",
    "METER_ID": "987654312",
    "DATA_DATE": "2021-11-01 00:00:00",
    "FLAG": "2",
    "DATA": 1
  },
  {
    "ID": "123456789",
    "METER_ID": "987654312",
    "DATA_DATE": "2021-11-01 00:15:00",
    "FLAG": "2",
    "DATA": 2
  },
  {
    "ID": "123456789",
    "METER_ID": "987654312",
    "DATA_DATE": "2021-11-01 00:30:00",
    "FLAG": "2",
    "DATA": 3
  },
  {
    "ID": "123456789",
    "METER_ID": "987654312",
    "DATA_DATE": "2021-11-01 00:45:00",
    "FLAG": "2",
    "DATA": 4
  },
  {
    "ID": "123456789",
    "METER_ID": "987654312",
    "DATA_DATE": "2021-11-01 00:60:00",
    "FLAG": "2",
    "DATA": 5
  }
]
ouxuyong commented 2 years ago

@953077065 This 'spec' can convert a single json into a json array. If you want to implement time calculation, you'd better write a custom implementation yourself. It is more difficult to use the existing implementation.

[
  {
    "operation": "shift",
    "spec": {
      "ID": "data.ID",
      "*": {
        "@": "data.&1"
      },
      "I*": {
        "@": "DATA"
      }
    }
  }, {
    "operation": "shift",
    "spec": {
      "DATA": {
        "*": {
          "@2": {
            "data": {
              "*": {
                "@": "[#5].&1"
              }
            }
          },
          "@": "[#2].&2"
        }
      }
    }
  }
]