简体   繁体   中英

Json array transformation with Jolt

I'm trying to use Jolt to transform from a JSON array to another one. It consists of a nested JSON array with no keys.

Here is my input:

[
  [
    "20190207101456",
    1,
    2,
    3
  ],
  [
    "20190207101456",
    4,
    5,
    6
  ]
]

And I would like to get the following output:

[
 {
  "timestamp": "20190207101456",
  "value1": 1,
  "value2" : 2,
  "value3" : 3
 },
 {
  "timestamp": "20190207101456",
  "value1": 4,
  "value2" : 5,
  "value3" : 6
 }
]

I was able to add the keys with this spec file for just a single nested array element:

[
  {
    "operation": "shift",
    "spec": {
      "0": "timestamp",
      "1": "value1",
      "2": "value2",
      "3": "value3"
    }
  }
]

But I don't know how to apply this for the outer JSON array.

Something like this should do what you want:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "0": "[&1].timestamp",
        "1": "[&1].value1",
        "2": "[&1].value2",
        "3": "[&1].value3"
      }
    }
  }
]

After comment to default empty arrays you can do this:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "0": "[&1].timestamp",
        "1": "[&1].value1",
        "2": "[&1].value2",
        "3": "[&1].value3"
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "*": {
        "TRASH": "",
        "value_before_timestamp": "static_value"
      }
    }
  },
  {
    "operation": "remove",
    "spec": {
      "*": {
        "TRASH": ""
      }
    }
  }
]

TRASH trick taken from here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM