4

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"
  }
]
Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
user393014
  • 445
  • 1
  • 8
  • 15

2 Answers2

3

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 http://jolt-demo.appspot.com/ is

enter image description here

Barbaros Özhan
  • 59,113
  • 10
  • 31
  • 55
0

You may consider another library Josson.

https://github.com/octomix/josson

Deserialization

Josson josson = Josson.fromJsonString(
    "[" +
    "  {" +
    "    \"foundduring\": \"D-DC\"," +
    "    \"user_type\": \"type1\"," +
    "    \"location\": \"location1\"" +
    "  }," +
    "  {" +
    "    \"foundduring\": \"D-DG\"," +
    "    \"user_type\": \"type2\"," +
    "    \"location\": \"location2\"" +
    "  }," +
    "  {" +
    "    \"foundduring\": \"D-DI\"," +
    "    \"user_type\": \"type3\"," +
    "    \"location\": \"location3\"" +
    "  }" +
    "]");
    

Transformation

JsonNode node = josson.getNode(
    "field(foundduring.caseValue('D-DC','CycleCount','D-DG','Pick','D-DI','Issue'))");
System.out.println(node.toPrettyString());

Output

[ {
  "foundduring" : "CycleCount",
  "user_type" : "type1",
  "location" : "location1"
}, {
  "foundduring" : "Pick",
  "user_type" : "type2",
  "location" : "location2"
}, {
  "foundduring" : "Issue",
  "user_type" : "type3",
  "location" : "location3"
} ]
Raymond Choi
  • 1,065
  • 2
  • 7
  • 8