Anusha
Anusha

Reputation: 673

create a new array using JOLT

I am trying to modify the JSON output using JOLT, I have been using it mostly to rename/remove fields until now.

I want to create a new object of existing 2 arrays as elements.

input.json

[
  {
    "features": [
      {
        "featureId": 1,
        "feature": "feature"
      }
    ],
    "product": [
      {
        "id": 1,
        "name": "name"
      }
    ]
  }
]

current spec json:

[{
    "operation": "shift",
    "spec": {
      "*": {
        "features": {
          "*": {
            "featureId": "[&3].&2.[&1].featureId",
            "feature": "[&3].&2.[&1].feature"
          }
        },
        "product": {
          "*": {
            "id": "[&3].&2.[&1].id",
            "name": "[&3].&2.[&1].name"
          }
        }
      }
    }
    }
  ]

expected output:

[
  {
    "newarray": {
      "features": [
        {
          "featureId": 1,
          "feature": "feature"
        }
      ],
      "product": [
        {
          "id": 1,
          "name": "name"
        }
      ]
    }
  }
]

Basically, I want to move both of my existing arrays inside a new object. Thanks in advance!

Upvotes: 0

Views: 4009

Answers (1)

Milo S
Milo S

Reputation: 4586

Spec

[
  {
    "operation": "shift",
    "spec": {
      "*": { // outer array index
        // match features and product which are lists
        // and copy those lists to the output
        "*": "[0].newarray.&"
      }
    }
  }
]

Upvotes: 1

Related Questions