Balaji211
Balaji211

Reputation: 307

Jolt Transform JSON Spec for Array Input

I am trying to do JOLT shift operation with below spec which is not working. Not sure what mistake I have done. Need help in this case. Output JSON is coming as an object instead of Array and shift also not working as expected.

    Input : [
      {
        "Header": {
          "Number": 1,
          "Id": "JO"
        },
        "Name": "John"
      },
      {
        "Header": {
          "Number": 2,
          "Id": "JS"
        },
        "Name": "Justin"
      }
    ]
    Spec : [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "Header": "Header",
            "Name": "Header.Name"
          }
        }
      }
    ]
    Expected Output : [
      {
        "Header": {
          "Number": 1,
          "Id": "JO",
          "Name": "John"
        }    
      },
      {
        "Header": {
          "Number": 2,
          "Id": "JS",
          "Name": "Justin"
        }    
      }
    ]
    Actual Output : {
      "Header" : [ {
        "Number" : 1,
        "Id" : "JO",
        "Name" : "John"
      }, {
        "Number" : 2,
        "Id" : "JS"
      } ]
    }

Upvotes: 2

Views: 1741

Answers (1)

kasptom
kasptom

Reputation: 2458

You have to also specify that the "Header" object is inside the array.

Moreover, the index of the array where you place the "Header" object for each of the element of the array.

That's what the spec below does (using the [&1] - apmersand wildcard combined with array):

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Header": "[&1].Header",
        "Name": "[&1].Header.Name"
      }
    }
  }
]

Sources:

  1. Shiftr.java javadocs:
  2. Other answer: "How do I transform an array using Jolt?"
  3. Demo application linked in the jolt repo to test the spec

Upvotes: 2

Related Questions