Home > Blockchain >  Jolt transform Json array keep rest of the fields
Jolt transform Json array keep rest of the fields

Time:04-25

How to keep other fields in the Jolt transform JSON array, I am trying to use wildcard but fields are not added in the final output?

Here is the example input I am using

[
  {
    "foundduring": "D-DC",
    "user_type": "type1",
    "location": "location1"
  },
  {
    "foundduring": "D-DG",
    "user_type": "type2",
    "location": "location2"
  },
  {
    "foundduring": "D-DI",
    "user_type": "type3",
    "location": "location3"
  }
]

I am using the following Jolt transformation and also trying wildcard:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "foundduring": {
          "D-DC": {
            "#CycleCount": "[&3].foundduring"
          },
          "D-DG": {
            "#Pick": "[&3].foundduring"
          },
          "D-DI": {
            "#Issue": "[&3].foundduring"
          }
        },
        "@": "&"
      }
    }
  }
]

Following is my expected output where shift operation happened and then need to keep all other fields as it it

[
   {
    "foundduring" : "CycleCount",
    "user_type" : "type1",
    "location" : "location1"
  },
   {
    "foundduring" : "Pick",
    "user_type" : "type2",
    "location" : "location2"
  },
   {
    "foundduring" : "Issue",
    "user_type" : "type3",
    "location" : "location3"
  }
]

Actual Output coming:

[
  {
    "foundduring": "CycleCount"
  },
  {
    "foundduring": "Pick"
  },
  {
    "foundduring": "Issue"
  }
]

CodePudding user response:

Consider using "*" wildcard as else case instead of "@" such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "foundduring": {
          "D-DC": {
            "#CycleCount": "[&3].&2"
          },
          "D-DG": {
            "#Pick": "[&3].&2"
          },
          "D-DI": {
            "#Issue": "[&3].&2"
          }
        },
        "*": "[&1].&"
      }
    }
  }
]

Btw, no need to get the key name "foundduring", just use &2 substitution to go 2 level up from the current branch and grab that value.

The demo on the site enter image description here

  • Related