skip to Main Content

I want to write a Jolt definition that loops through each object in my attachment array, and move the object to the data array. At the same time, it should map single object attachment nest, and move the object to the data object.

Currently my spec below is working when my attachment block is an array.

Input :

{
  "IntegrationEntities": {
    "integrationEntity": [
      {
        "integrationEntityHeader": {
          "attachments": {
            "attachment": [
              {
                "name": "EV10044.docx"
              },
              {
                "name": "Test1.txt"
              }
            ]
          }
        }
      }
    ]
  }
}

JOLT Spec :

[
  {
    "operation": "shift",
    "spec": {
      "IntegrationEntities": {
        "integrationEntity": {
          "*": {
            "integrationEntityHeader": {
              "attachments": {
                "attachment": {
                  "*": {
                    "name": "data[&1].filename"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

Current Output :

{
  "data": [
    {
      "filename": "EV10044.docx"
    },
    {
      "filename": "Test1.txt"
    }
  ]
}

I want to handle the scenario in a way where regardless if the attachment is an array or object it should give the input. Currently it fails if the attachment block is an object and gives the output as NULL.

IF Input is:

{
  "IntegrationEntities": {
    "integrationEntity": [
      {
        "integrationEntityHeader": {
          "attachments": {
            "attachment": {
              "name": "EV10044.docx"
            }
          }
        }
      }
    ]
  }
}

Desired Output:

{
  "data": {
    "filename": "EV10044.docx"
  }
}

2

Answers


  1. You can use this spec:

    [
      {
        "operation": "shift",
        "spec": {
          "IntegrationEntities": {
            "integrationEntity": {
              "*": {
                "integrationEntityHeader": {
                  "attachments": {
                    "attachment": {
                      "name": "data.filename",
                      "*": {
                        "name": "data[&1].filename"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    ]
    

    I just added the below line to your spec. So if you have name in the attachment it is assumed that you have 1 attachment. If no name is found it is assumed you have an array and your other codes is correct.

    "name": "data.filename",
    
    Login or Signup to reply.
  2. An option is to use a shift transformation spec in which the objects are qualified by path expressions and the arrays are constructed along with sub-indexes by symbolic expression "*": { such as

    [
      {
        "operation": "shift",
        "spec": {
          "@IntegrationEntities.integrationEntity": {
            "*": { // indexes of the array
              "@integrationEntityHeader.attachments.attachment": {
                "*": { // indexes of the array
                  "*": "data[].file&", // if "attachment" is an array
                  "@1,name": "data.file&" // if "attachment" is an object
                }
              }
            }
          }
        }
      }
    ]
    

    Btw, if you didn’t need to rename the innermost attribute name to fieldname, the following shorter spec would suffice :

    [
      {
        "operation": "shift",
        "spec": {
          "@IntegrationEntities.integrationEntity": {
            "*": {
              "@integrationEntityHeader.attachments.attachment": "data"
            }
          }
        }
      }
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search