DEV Community

Cover image for Mastering JOLT Spec Operations: A Guide with Examples
Sunil Yaduvanshi
Sunil Yaduvanshi

Posted on

Mastering JOLT Spec Operations: A Guide with Examples

Note: For the practical exposure you can visit below link

https://jolt-demo.appspot.com/#inception

JOLT (JSON to JSON Transformation) is a powerful framework for transforming JSON data. It’s especially useful when dealing with complex transformations such as restructuring, renaming, or removing JSON fields. In this blog, I will walk you through some key JOLT operations and how to use the special operators &, *, @, $, #, along with transformation operations like shift, remove, and cardinality.

To demonstrate these operations, we’ll use the following sample JSON as our input:

Input JSON:

{
  "client": {
    "address": {
      "street": "11 street cop work",
      "state": "New York"
    },
    "name": "Amazon",
    "contact": "001–786543",
    "employeeDetails": [
      {
        "name": "John Green",
        "designation": "Software engineer",
        "phone": "91–7865432300",
        "home": "080–78645–87362",
        "address": "11th streem Smt Amest"
      },
      {
        "name": "Dev Axe",
        "designation": "Sr. Software engineer",
        "phone": "91–7865432900",
        "home": "080–78645–89362",
        "address": "11th Smt Amest"
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

1. The & Operator: Accessing Indices or Values

The & operator allows you to reference the current index or value when transforming arrays.

Example: Mapping Employee Names with Indices

[
  {
    "operation": "shift",
    "spec": {
      "client": {
        "employeeDetails": {
          "*": {
            "name": "employees[&1].name"
          }
        }
      }
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "employees": [
    { "name": "John Green" },
    { "name": "Dev Axe" }
  ]
}
Enter fullscreen mode Exit fullscreen mode

In this example, &1 references the index of the current employee in the array.


2. The * Operator: Wildcard Matching

* is a wildcard operator used to match any key or value. This is useful for applying the same transformation to multiple fields or elements.

Example: Shifting Employee Data to a New Structure

[
  {
    "operation": "shift",
    "spec": {
      "client": {
        "employeeDetails": {
          "*": {
            "name": "employees[].name",
            "designation": "employees[].role"
          }
        }
      }
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "employees": [
    { "name": "John Green", "role": "Software engineer" },
    { "name": "Dev Axe", "role": "Sr. Software engineer" }
  ]
}
Enter fullscreen mode Exit fullscreen mode

Here, * matches all the employee objects, ensuring each one is transformed into a new structure with the name and role.


3. The @ Operator: Self-Reference

The @ operator lets you refer to the current value being processed. You can use it to copy the entire structure at the current level.

Example: Copying Employee Data into a Separate Field

[
  {
    "operation": "shift",
    "spec": {
      "client": {
        "employeeDetails": {
          "*": {
            "name": "employees[].name",
            "@": "employees[].originalData"
          }
        }
      }
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "employees": [
    {
      "name": "John Green",
      "originalData": {
        "name": "John Green",
        "designation": "Software engineer",
        "phone": "91–7865432300",
        "home": "080–78645–87362",
        "address": "11th streem Smt Amest"
      }
    },
    {
      "name": "Dev Axe",
      "originalData": {
        "name": "Dev Axe",
        "designation": "Sr. Software engineer",
        "phone": "91–7865432900",
        "home": "080–78645–89362",
        "address": "11th Smt Amest"
      }
    }
  ]
}
Enter fullscreen mode Exit fullscreen mode

In this case, @ duplicates the entire employee object into a new field called originalData.


4. The $ Operator: Accessing Root-Level Data

The $ operator gives you access to the root level of the JSON document, allowing you to bring in data from other parts of the document.

Example: Copying Client Contact into Each Employee Record

[
  {
    "operation": "shift",
    "spec": {
      "client": {
        "employeeDetails": {
          "*": {
            "name": "employees[&1].name",
            "$": "employees[&1].clientContact"
          }
        }
      }
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "employees" : [ {
    "clientContact" : "0",
    "name" : "John Green"
  }, {
    "clientContact" : "1",
    "name" : "Dev Axe"
  } ]
}
Enter fullscreen mode Exit fullscreen mode

The $ operator allows us to access the root level and bring in the client’s contact information for each employee.


5. The # Operator: Inserting Literal Values

With #, you can insert literal values directly into the output, which is useful for providing default values.

Example: Inserting a Default Email for Employees

[
  {
    "operation": "shift",
    "spec": {
      "client": {
        "employeeDetails": {
          "*": {
            "name": "employees[].name",
            "#Not Available": "employees[].email"
          }
        }
      }
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "employees" : [ {
    "email" : "Not Available"
  }, {
    "name" : "John Green"
  }, {
    "email" : "Not Available"
  }, {
    "name" : "Dev Axe"
  } ]
}
Enter fullscreen mode Exit fullscreen mode

Here, #Not Available adds the literal string "Not Available" for employees who don’t have an email field.


6. Shift Operation: Restructuring JSON

The shift operation is one of the most commonly used transformations. It allows you to move data to new keys or structures.

Example: Restructuring Employee Data

[
  {
    "operation": "shift",
    "spec": {
      "client": {
        "employeeDetails": {
          "*": {
            "name": "transformedEmployees[].fullName",
            "designation": "transformedEmployees[].jobTitle"
          }
        }
      }
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "transformedEmployees" : [ {
    "fullName" : "John Green"
  }, {
    "jobTitle" : "Software engineer"
  }, {
    "fullName" : "Dev Axe"
  }, {
    "jobTitle" : "Sr. Software engineer"
  } ]
}
Enter fullscreen mode Exit fullscreen mode

The shift operation moves the employee details into a new key (transformedEmployees).


7. Remove Operation: Deleting Data

The remove operation helps you eliminate unwanted fields from your JSON.

Example: Removing Sensitive Employee Data

[
  {
    "operation": "remove",
    "spec": {
      "client": {
        "employeeDetails": {
          "*": {
            "home": ""
          }
        }
      }
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Output:

{
  "client" : {
    "address" : {
      "state" : "New York",
      "street" : "11 street cop work"
    },
    "contact" : "001?786543",
    "employeeDetails" : [ {
      "address" : "11th streem Smt Amest",
      "designation" : "Software engineer",
      "name" : "John Green",
      "phone" : "91?7865432300"
    }, {
      "address" : "11th Smt Amest",
      "designation" : "Sr. Software engineer",
      "name" : "Dev Axe",
      "phone" : "91?7865432900"
    } ],
    "name" : "Amazon"
  }
}
Enter fullscreen mode Exit fullscreen mode

The home field has been removed from each employee’s contact details.


8. Cardinality: Converting Arrays to Objects

The cardinality operation allows you to change how arrays are represented. You can collapse arrays with a single item into objects.

Example: Converting Employee Array to Single Object

[
  {
    "operation": "cardinality",
    "spec": {
      "client": {
        "employeeDetails": "ONE"
      }
    }
  }
]
Enter fullscreen mode Exit fullscreen mode

Output (if there is only one employee):

{
  "client" : {
    "address" : {
      "state" : "New York",
      "street" : "11 street cop work"
    },
    "contact" : "001?786543",
    "employeeDetails" : {
      "address" : "11th streem Smt Amest",
      "designation" : "Software engineer",
      "home" : "080?78645?87362",
      "name" : "John Green",
      "phone" : "91?7865432300"
    },
    "name" : "Amazon"
  }
}
Enter fullscreen mode Exit fullscreen mode

This collapses the employeeDetails array into a single object if it contains only one employee.


Conclusion

The JOLT framework provides a wide range of powerful transformations for working with JSON data. Whether you need to restructure data, remove sensitive fields, or insert default values, these operations allow you to make quick, effective changes.

Top comments (0)