J.Zil
J.Zil

Reputation: 2449

Mapping array value to JSON in JOLT

This is my JSON example:

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

This is my JOLT:

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

What it outputs is:

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

However what I am trying to output is this:

[ {
  "date" : "00:00 2019-06-03",
  "metric1" : 120
  "metric2" : 10
}, {
  "date" : "05:00 2019-06-08",
  "metric1" 98
  "metric2: 12
}, {
  "date" : "23:00 2019-06-09",
  "metric1" : 172
  "metric2" : 8
} ]

How do I map to specific array values rather than it returning the whole lot? It seems like it is around the wildcard on the metric1 line to select the first item but I cant find any code which does this.

Upvotes: 0

Views: 455

Answers (1)

Udith Gunaratna
Udith Gunaratna

Reputation: 2111

When you use the wildcard as below, it means that every item of data array should be mapped to the key [&2].metric1, which makes metric1 field an array.

"data": {
    "*": "[&2].metric1"
}

What you should do is map the 1st element (element at index 0) of data array to metric1 key and 2nd element (element at index 1) to metric2 key as follows.

[
{
    "operation": "shift",
    "spec": {
        "rows": {
            "*": {
                "value": "[&1].date",
                "data": {
                    "0": "[&2].metric1",
                    "1": "[&2].metric2"
                }
            }
        }
    }
}
]

Upvotes: 1

Related Questions