Quantcast
Channel: Blog | Object Partners
Viewing all articles
Browse latest Browse all 93

Jolt transform both fields required

$
0
0

Jolt is a JSON to JSON transformation library where the transform is defined in JSON. As an example you may want to transform the latitude and longitude fields into a location object containing those fields.

Transform
[
  {
    "operation": "shift",
    "spec": {
      "latitude":"location.latitude",
      "longitude": "location.longitude"              
    }
  }
]
Input
{
  "latitude": "41.2565",
  "longitude": "-95.9345"
}
Output
{
  "location": {
    "latitude": "41.2565",
    "longitude": "-95.9345"
  }
}

Sometimes you only want to transform two fields if both values are present. A location isn’t really a location if it only has a latitude or a longitude. In that case you can nest the transform like this.

Both Required Transform
[
  {
    "operation": "shift",
    "spec": {
      "latitude": {
        "@(1,longitude)": {
          "@(2,latitude)": "location.latitude",
          "@(2,longitude)": "location.longitude"
        }
      }
    }
  }
]

What’s going on here? Well we are mapping the latitude to a block of transform. That transform maps “@(1, longitude)” –go up a level and get the longitude field– to another block that maps up two levels to latitude and up two levels to longitude to the “location.latitude” and “location.longitude” respectively. Now we are only mapping the data if both latitude and longitude exist!


Viewing all articles
Browse latest Browse all 93

Trending Articles