Search code examples
apache-nifijolt

Unable to transform into below format json using Jolt transformation


Unable to achieve above output format using jolt and gone through multiple SO questions and could not find similar one. Tried with adding indexes inside array of jolt spec but did not work. Thanks in Advance and find the input, output and jolt spec at below

Input:

{
  "test1": "Student",
  "School": {
    "Syllabus": {
      "Midterm": {
        "inclusions": {
          "includedSubjectsList": {
            "Subjects": [
              {
                "subjectName": "MH1"
              },
              {
                "subjectName": "MH2"
              },
              {
                "subjectName": "MH3"
              },
              {
                "subjectName": "MH4"
              }
            ]
          }
        }
      }
    }
  }
}

Jolt Spec:

[
  {
    "operation": "shift",
    "spec": {
      "School": {
        "Syllabus": {
          "Midterm": {
            "inclusions": {
              "includedSubjectsList": {
                "Subjects": {
                  "*": {
                    "subjectName": "Academy[].books[]"
                  }
                }
              }
            }
          }
        }
      }
    }
  }
]

Current output:

{
  "Academy": [
    {
      "books": [
        "MH1"
      ]
    },
    {
      "books": [
        "MH2"
      ]
    },
    {
      "books": [
        "MH3"
      ]
    },
    {
      "books": [
        "MH4"
      ]
    }
  ]
}

Expected output:

{
  "Academy": [
    {
      "books": [
        "MH1",
        "MH2",
        "MH3",
        "MH4"
      ]
    }
  ]
}

Solution

  • You are almost right. Replace

    "subjectName": "Academy[].books[]"
    

    with

    "subjectName": "Academy[0].books[]"