Feb 4, 2021

Jolt transform both fields required

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!

About the Author

Scott Bock profile.

Scott Bock

Principal Technologist

Scott is a Senior Software Engineer with over 12 years of experience using Java, and 5 years experience in technical leadership positions. His strengths include troubleshooting and problem solving abilities, excellent repertoire with customers and management, and verbal and written communication. He develops code across the entire technology stack including database, application, and user interface.

One thought on “Jolt transform both fields required

  1. Scott Bock says:

    See full code example with test at https://github.com/scottbock/Jolt

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Infrastructure as Code – The Wrong Way
You are probably familiar with the term “infrastructure as code”. It’s a great concept, and it’s gaining steam in the industry. Unfortunately, just as we had a lot to learn about how to write clean […]
Snowflake CI/CD using Jenkins and Schemachange
CI/CD and Management of Data Warehouses can be a serious challenge. In this blog you will learn how to setup CI/CD for Snowflake using Schemachange, Github, and Jenkins. For access to the code check out […]
How to get your pull requests approved more quickly
TL;DR The fewer reviews necessary, the quicker your PR gets approved. Code reviews serve an essential function on any software codebase. Done right, they help ensure correctness, reliability, and maintainability of code. On many teams, […]
Kafka & Kubernetes: Scaling Consumers
Kafka and Kubernetes (K8s) are a great match. Kafka has knobs to optimize throughput and Kubernetes scales to multiply that throughput. On the consumer side, there are a few ways to improve scalability. Resource & […]