JOLT Transformation: How to Conditionally Merge Two Fields and Retain Other Attributes?

I’m working on a JOLT transformation where I need to merge two fields, AccountNumber and EntityID, retaining only the non-empty value. In my input, one of these fields will always have a value, but never both. Additionally, I want to keep other relevant attributes in the output.

Here’s an example of my simplified input JSON with dummy data:

{
  "AccountNumber": "123456",
  "EntityID": "",
  "TransactionYear": "2023",
  "TransactionMonth": "03",
  "TransactionDay": "05",
  "TransactionHour": "10",
  "TransactionMinutes": "15",
  "TransactionSeconds": "30",
  "ComponentNumber": "12345",
  "Price": "250.00",
  "CurrencyCode": "USD"
}

Objective:
Merge AccountNumber and EntityID, keeping only the non-empty value. Since one of these fields will always contain a value, I want the output to reflect that without creating an array.
Retain other attributes: Keep TransactionYear, TransactionMonth, TransactionDay, TransactionHour, TransactionMinutes, TransactionSeconds, ComponentNumber, Price, and CurrencyCode in the output.

Here’s the JOLT spec I’m using:

[
  {
    "operation": "shift",
    "spec": {
      "AccountNumber": "Temp.AccountNumber",
      "EntityID": "Temp.EntityID",
      "TransactionYear": "TransactionData.TransactionYear",
      "TransactionMonth": "TransactionData.TransactionMonth",
      "TransactionDay": "TransactionData.TransactionDay",
      "TransactionHour": "TransactionData.TransactionHour",
      "TransactionMinutes": "TransactionData.TransactionMinutes",
      "TransactionSeconds": "TransactionData.TransactionSeconds",
      "ComponentNumber": "NewPrice.ComponentNumber",
      "Price": "NewPrice.SpecialPrice.Price",
      "CurrencyCode": "NewPrice.SpecialPrice.CurrencyCode"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "AccountNumber": "=firstNotNull(@(1,Temp.AccountNumber), @(1,Temp.EntityID))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "Temp": ""
    }
  }
]

Problem:
The output I’m getting still contains AccountNumber as an array:

{
  "AccountNumber": ["","123456"],
  "TransactionData": {
    "TransactionYear": "2023",
    "TransactionMonth": "03",
    "TransactionDay": "05",
    "TransactionHour": "10",
    "TransactionMinutes": "15",
    "TransactionSeconds": "30"
  },
  "NewPrice": {
    "ComponentNumber": "12345",
    "SpecialPrice": {
      "Price": "250.00",
      "CurrencyCode": "USD"
    }
  }
}

Desired Output:
I want AccountNumber to be a single value (either AccountNumber or EntityID), not an array, while retaining all other attributes:

{
  "AccountNumber": "123456",
  "TransactionData": {
    "TransactionYear": "2023",
    "TransactionMonth": "03",
    "TransactionDay": "05",
    "TransactionHour": "10",
    "TransactionMinutes": "15",
    "TransactionSeconds": "30"
  },
  "NewPrice": {
    "ComponentNumber": "12345",
    "SpecialPrice": {
      "Price": "250.00",
      "CurrencyCode": "USD"
    }
  }
}

Question:
How can I modify my JOLT spec to ensure that only the first non-empty value between AccountNumber and EntityID is retained without resulting in an array, while still keeping all other attributes?

Thank you in advance!

You can use the following transformation spec :

[
  {
    "operation": "shift",
    "spec": {
      "AccountNumber|EntityID": "Temp.AccountNumber",//form the array named "AccountNumber"
      "*tion*": "&(0,1)tionData.&", //TransactionData -> replicate the first 
                                    //asterisk, eg. "Transact" by &(0,1) 
      "*": "NewPrice.&" //others
    }
  },
  {//combine the components of the "AccountNumber" array 
    "operation": "modify-overwrite-beta",
    "spec": {
      "Temp": {
        "AccountNumber": "=join('',@(1,&))"
      }
    }
  }
]

the demo on the site Jolt Transform Demo Using v0.1.1 is :

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật