J.Zil
J.Zil

Reputation: 2449

Add default field to Jolt JSON transformation

I'm trying to make a JSON transformation using JOLT and I think I'm close, but what I can't do is add in a new field to each one which doesn't currently appear.

I have read quite a few tutorials and this code below is what I would have expected to have worked. However it doesn't seem to add in the new "Name" field.

{
  "totalElements": 168,
  "columns": {
    "dimension": {
      "id": "variables/daterangehour",
      "type": "time"
    },
    "columnIds": [
      "1"
    ]
  },
  "rows": [
    {
      "itemId": "119050300",
      "value": "00:00 2019-06-03",
      "data": [
        120
      ]
    },
    {
      "itemId": "119050805",
      "value": "05:00 2019-06-08",
      "data": [
        98
      ]
    },
    {
      "itemId": "119050923",
      "value": "23:00 2019-06-09",
      "data": [
        172
      ]
    }
  ]
}

This is my jolt:

[
  {
    "operation": "default",
    "spec": {
      "name": "chart1"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "rows": {
        "*": {
          "value": "[&1].date",
          "data": {
            "*": "[&2].data"
          }
        }
      }
    }
  }
 ]

This was output:

[ {
  "date" : "00:00 2019-06-03",
  "data" : 120
}, {
  "date" : "05:00 2019-06-08",
  "data" : 98
}, {
  "date" : "23:00 2019-06-09",
  "data" : 172
} ]

I wanted it to be like this though:

[ {
  "name" : "graph1",
  "date" : "00:00 2019-06-03",
  "data" : 120
}, {
  "name" : "graph1",
  "date" : "05:00 2019-06-08",
  "data" : 98
}, {
  "name" : "graph1",
  "date" : "23:00 2019-06-09",
  "data" : 172
} ]

Can anyone please tell me where I am going wrong? Apparently default is used to add in a new item but it doesnt seem to do anything.

Upvotes: 0

Views: 1138

Answers (1)

Magda
Magda

Reputation: 482

You should put sth like this: "#chart1": "[&1].name" Hash puts harcoded value and then you do not need operation default. Or if you want to use operation default:

[
  {
    "operation": "shift",
    "spec": {
      "rows": {
        "*": {
          "value": "[&1].date",
          "data": {
            "*": "[&2].data"
          }
        }
      }
    }
  },
  {
    "operation": "default",
    "spec": {
      "*": {
        "name": "chart1"
      }
    }
  }
]

Upvotes: 1

Related Questions