Search code examples
jsonjolt

Need jolt spec for the json input(nested array) and output


I have an input JSON which has nested array. I need jolt spec file which will allow only the specific fields in the output json and rest will be ignored. So I need to specify each field so that only those fields will be displayed in output and any extra unwanted field will be ignored.

The jolt spec I write is giving me all the elements together for nested array section ,under tag "drops" like below but

"drops" : [ {
          "gross" : [ "12345", "9876", "4567" ],
          "amount" : [ "098", "12", "08798" ],
          "actual" : [ 0, 0, 0 ]
        } ]

but I need it like

 "drops": [
            {
              "gross": "12345",
              "amount": "098",
              "actual": 0
            }
          ]

Input Json:

{
  "metadata": {
    "version": "1"
  },
  "message": {
    "id": "0987",
    "code": "ABCD",
    "CHECK": {
      "id": "6578",
      "date": "2024-05-16",
      "summary": [
        {
          "actual": "0",
          "type": "processed"
        },
        {
          "actual": "1",
          "type": "cancel"
        }
      ],
      "group_summary": {
        "total": 7,
        "cancel": 5,
        "hold": 1
      },
      "shift": [
        {
          "id": "09876",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "12345",
              "amount": "098",
              "actual": 0
            }
          ]
        },
        {
          "id": "09875",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "9876",
              "amount": "12",
              "actual": 0
            }
          ]
        },
        {
          "id": "45678",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "4567",
              "amount": "08798",
              "actual": 0
            }
          ]
        }
      ],
      "occupy": 1,
      "ownership": 2
    },
    "timezone": ""
  }
}

Output json That I need:

{
  "metadata": {
    "version": "1"
  },
  "message": {
    "id": "0987",
    "code": "ABCD",
    "CHECK": {
      "id": "6578",
      "date": "2024-05-16",
      "summary": [
        {
          "actual": "0",
          "type": "processed"
        },
        {
          "actual": "1",
          "type": "cancel"
        }
      ],
      "group_summary": {
        "total": 7,
        "cancel": 5,
        "hold": 1
      },
      "shift": [
        {
          "id": "09876",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "12345",
              "amount": "098",
              "actual": 0
            }
          ]
        },
        {
          "id": "09875",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "9876",
              "amount": "12",
              "actual": 0
            }
          ]
        },
        {
          "id": "45678",
          "date": "2024-05-16",
          "drops": [
            {
              "gross": "4567",
              "amount": "08798",
              "actual": 0
            }
          ]
        }
      ],
      "occupy": 1,
      "ownership": 2
    },
    "timezone": ""
  }
}

Jolt spec I tried

[
  {
    "operation": "shift",
    "spec": {
      "metadata": {
        "version": ["metadata.version"]
      },
      "message": {
        "id": ["message.id"],
        "code": ["message.code"],
        "CHECK": {
          "id": ["message.CHECK.id"],
          "date": ["message.CHECK.date"],
          "summary": {
            "*": {
              "actual": "message.CHECK.summary[&1].actual",
              "type": "message.CHECK.summary[&1].type"
            }
          },
          "group_summary": {
            "total": "message.CHECK.group_summary[&1].total",
            "cancel": "message.CHECK.group_summary[&1].cancel",
            "hold": "message.CHECK.group_summary[&1].hold"
          },
          "shift": {
            "*": {
              "id": "message.CHECK.shift[&1].id",
              "date": "message.CHECK.shift[&1].date",
              "drops": {
                "*": {
                  "gross": "message.CHECK.shift[&1].drops[&1].gross",
                  "amount": "message.CHECK.shift[&1].drops[&1].amount",
                  "actual": "message.CHECK.shift[&1].drops[&1].actual"
                }
              }
            }
          },
          "occupy": ["message.CHECK.occupy"],
          "ownership": ["message.CHECK.ownership"]
        },
        "timezone": ["message.timezone"]
      }
    }
  }
]



Solution

  • Seems that you only want to return the first component of the shift array, so just pick the zero index under "shift" node such as

      "shift": {
        "0": {
          "id": "message.CHECK.shift[&1].id",
          "date": "message.CHECK.shift[&1].date",
          "drops": {
            "*": {
              "gross": "message.CHECK.shift[&1].drops[&1].gross",
              "amount": "message.CHECK.shift[&1].drops[&1].amount",
              "actual": "message.CHECK.shift[&1].drops[&1].actual"
            }
          }
        }
      }
    

    or shortly, symbolically :

      "shift": {
        "0": {
          "*": "&4.&3.&2[&1].&",
          "drops": {
            "*": {
              "*": "&6.&5.&4[&1].&2[&1].&"
            }
          }
        }
      }