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"
  }
]

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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"
} ]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.